gpt4 book ai didi

mysql - 执行此 SQL 查询的更简洁方法

转载 作者:行者123 更新时间:2023-12-01 00:27:59 25 4
gpt4 key购买 nike

所以有一个客户列表,有一个客户列表。

我有 2 个表,customersinvoices

Customer_id 可以与我的客户相同,例如

客户A

Customer #1

客户端B

Customer #1

因此对于不同的客户,发票表的 customer_id 可以为 1。

下面的查询有效,但看起来有点困惑。有什么清理它的想法吗?

  SELECT   name, 
sum(sub_total) total
FROM customers, invoices
WHERE customer_id = 1438 and
invoices.customer_id = 1438 and
customers.customer_id = invoices.customer_id
GROUP BY name
Order By total DESC;

最佳答案

如果 A = BA = 1438 那么 B = 1438 你不需要检查它......
(相信我,我在高中时的数学成绩为 60+)

SELECT   name, sum(sub_total) total    
FROM customers, invoices
WHERE invoices.customer_id = 1438
AND customers.customer_id = invoices.customer_id
GROUP BY name
ORDER BY total DESC;

或者明确说明您想要哪种类型的 JOIN:

SELECT   name, sum(sub_total) total    
FROM customers INNER JOIN invoices
ON customers.customer_id = invoices.customer_id
WHERE invoices.customer_id = 1438
GROUP BY name
ORDER BY total DESC;

关于mysql - 执行此 SQL 查询的更简洁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576317/

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