【发布时间】:2014-01-11 09:01:08
【问题描述】:
我有以下 SQL 查询:
SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no, m.winnings
FROM users
LEFT JOIN tahminler ON users.id = tahminler.user_id
LEFT JOIN matches_of_comments ON tahminler.match_id = matches_of_comments.match_id
LEFT JOIN (SELECT user_id ,count(result) as winnings from tahminler WHERE result = 1 group by user_id) as m ON m.user_id = users.id
WHERE (MONTH( STR_TO_DATE( matches_of_comments.match_date, '%d.%m.%Y' ) ) = 01 AND YEAR( STR_TO_DATE( matches_of_comments.match_date, '%d.%m.%Y' ) ) = 2014 AND flag=1)
GROUP BY users.id
having count(tahminler.tahmin) > 0
Where 子句不适用于子查询 (m) 。我不想在子查询中添加相同的子句,这会使查询变得复杂且未优化。有没有办法在子查询中应用这个条件而不在子查询中重复它
【问题讨论】:
标签: mysql sql database subquery query-optimization