我有以下SQL代碼;重新使用父語句中的嵌套SQL語句在MySQL
SELECT a.*, b.maxDate as last_payment_date, c.package as payment_for_package, (SELECT COUNT(id) FROM uploads WHERE user = a.id) AS upload_count
FROM users a
INNER JOIN payments c
ON a.id = c.user_id
INNER JOIN
(
SELECT user_id, MAX(date) maxDate, period
FROM payments
GROUP BY user_id
) b ON c.user_id = b.user_id AND
c.date = b.maxDate
WHERE a.package = 1
AND b.maxDate < $twomonths
AND (SELECT COUNT(id) FROM uploads WHERE user = a.id) > 10
AND b.period = 1
ORDER BY b.maxDate ASC
LIMIT 50
正如你可以看到我使用嵌套的語句(SELECT COUNT(id) FROM uploads WHERE user = a.id)
兩次,我想知道如果有一種方法可以重新使用該語句,以提高性能?我已經使用別名upload_count
嘗試,但你不能在WHERE
條款使用別名。感謝幫助
也許你應該備份,並詢問如何寫一個好的查詢來獲取數據了(張貼的模式和一些相同的數據)。很難相信,這個查詢是可以做的最好的,不管你是否可以重複使用它的一部分。 –