gpt4 book ai didi

mysql - 使用 mysqlQuery 重复某些值时未获得预期结果

转载 作者:行者123 更新时间:2023-11-29 15:15:45 24 4
gpt4 key购买 nike

我正在尝试从 4 个表中获取数据,并且我编写了一个查询

SELECT
c.id,
c.name,
c.price,
c.duration,
c.period,
c.practical_classes,
c.theory_classes,
e.id,e.created_at,
u.id,
u.registration_no,
i.id,
i.amountpaid,
(i.amount - i.amountpaid) as balanceamount
FROM
courses c,
coursesenrolled e,
users u,
invoices i
WHERE e.student = "11"
AND e.course = c.id
AND c.delete_status = "0"
AND c.status = "Available"
AND u.id = "11"
AND i.student = e.student

我得到的结果如下图所示,这里原始数据是重复的。如果我在查询中添加 GROUP BY,最后 3 列会得到错误的数据 result after executing query without adding Group By

如果我添加分组依据,我会得到以下结果 result with Group By in query

下面我附上表格格式1.“类(class)”, table name: course2.“coursesenrolled”正在处理哪个类(class)分配给哪个用户的数据 courses enrollled3.“用户” enter image description here enter image description here4.发票,为用户办理发票 enter image description here

最佳答案

我尝试理解这个问题,所以你想获得 id=11 用户的结果,对吗?试试这个

SELECT DISTINCT c.id,c.name,c.price,c.duration,c.period,c.practical_classes,c.theory_classes,e.id,e.created_at,u.id,u.registration_no,i.id,i.amountpaid,(i.amount-i.amountpaid) as balanceamount FROM courses AS c INNER JOIN coursesenrolled AS e ON e.course=c.id INNER JOIN users AS u ON u.id=e.student INNER JOIN invoices AS i ON i.student=u.id  WHERE u.id='11' AND c.delete_status='0' AND c.status='Available';

关于mysql - 使用 mysqlQuery 重复某些值时未获得预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701938/

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