gpt4 book ai didi

PHP/MySQL : How to compare the sum of certain fields on a query to a number?

转载 作者:行者123 更新时间:2023-11-29 20:40:00 26 4
gpt4 key购买 nike

我有一个可以注册类(class)并付费的用户列表。

我想要列出这些用户以及他们注册的类(class),并显示他们已经为每门类(class)支付了多少钱。

这些是我的表格:

CREATE TABLE usuarios(
userID int unsigned not null auto_increment primary key,
userEmail char(50) null,
userDNI int(10) null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE rolesUsuarios (
rolesUsuariosID int unsigned not null auto_increment primary key,
userID int not null,
nombreRol char(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE cursos (
cursoID int unsigned not null auto_increment primary key,
nombreCurso char(100) not null,
cursoPrecio int(10) null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE cursosUsuarios (
cursosUsuariosID int unsigned not null auto_increment primary key,
userID int not null,
cursoID int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE pagos (
pagoID int unsigned not null auto_increment primary key,
userID int not null,
pagoMonto int null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE pagosVerificados (
pagosVerificadosID int unsigned not null auto_increment primary key,
userID int not null,
pagoID int not null,
cursoID int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

这是我正在使用的查询:

SELECT usuarios.userID AS useridd, usuarios.userEmail, usuarios.userApellido, rolesUsuarios.userID, rolesUsuarios.nombreRol,
cursos.cursoID, cursos.nombreCurso,
cursosUsuarios.cursoID, cursosUsuarios.userID,
GROUP_CONCAT(DISTINCT rolesUsuarios.nombreRol SEPARATOR '||') AS 'roles',
GROUP_CONCAT(DISTINCT cursos.cursoID,' - ',cursos.nombreCurso SEPARATOR '||') AS 'cursos',
GROUP_CONCAT(DISTINCT '$ ',pagos.pagoMonto,' course ID ',pagosVerificados.cursoID,'payment ID',pagos.pagoID ORDER BY pagos.pagoID SEPARATOR '||') AS 'pagos'

FROM usuarios LEFT JOIN rolesUsuarios ON usuarios.userID = rolesUsuarios.userID
LEFT JOIN cursosUsuarios ON usuarios.userID = cursosUsuarios.userID
LEFT JOIN cursos ON cursosUsuarios.cursoID = cursos.cursoID
LEFT JOIN roles ON rolesUsuarios.nombreRol = roles.nombreRol
LEFT JOIN pagosVerificados
ON rolesUsuarios.userID = pagosVerificados.userID
LEFT JOIN pagos
ON pagosVerificados.userID = pagos.userID
AND pagosVerificados.pagoID = pagos.pagoID

GROUP BY useridd

这是我得到的结果:同一列表中所有付款的列表,我想根据每个类(class)将它们分开。

Issue

最终目标是能够将每门类(class)的价格与每门类(class)的所有付款总和进行比较,并了解任何类(class)是否存在任何债务。

我该怎么做?我可以直接使用 PHP 和该查询来做到这一点,还是应该修改查询?

最佳答案

同样,我不会说这是什么语言,但我会从这个开始,并根据需要连接到其他表。

select A.userID, A.cursoID, B.cursoPrecio, IFNULL(sums.totalpaid, 0) `totalpaid`, (B.cursoPrecio - IFNULL(sums.totalpaid, 0)) `amount left`
from `cursosusuarios` A
inner join `cursos` B on (A.cursoID = B.cursoID)
left outer join (
select A.userID, A.cursoID, sum(B.pagoMonto) `totalpaid`
FROM `pagosverificados` A
inner join `pagos` B on (A.userID = B.userID and A.pagoID = B.pagoID)
group by A.userID, A.cursoID ) sums on (A.userID = sums.userID AND A.cursoID = sums.cursoID)

关于PHP/MySQL : How to compare the sum of certain fields on a query to a number?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38665096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com