Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用mysql。当我像这样使用 JOINS
SELECT u.Id,u.FirstName,u.LastName,u.Email,SUM(a.Amount) Amount FROM users u LEFT JOIN amount a WHERE a.ExpenseDate >= '2013-05-12' GROUP BY u.Id
我有 23 个用户,但它只显示 14 个用户数据,因为其余 9 个没有输入金额。我需要所有 23 个用户的数量。
移动ON子句中的条件,
ON
SELECT u.Id,u.FirstName,u.LastName,u.Email,SUM(a.Amount) Amount FROM users u LEFT JOIN amount a ON i.ID = a.ID AND a.ExpenseDate >= '2013-05-12' GROUP BY u.Id
在这种情况下,优化器amount会先过滤表中的记录,然后再将其加入表中user。
amount
user