我想用SUM()和LIMIT做一個子查詢。這與以下代碼正常工作:MySQL子查詢SUM限制
SELECT id,
(
SELECT SUM(number)
FROM (
SELECT number
FROM t2
WHERE u_id = '1'
ORDER BY time ASC
LIMIT 30
) AS temp
) AS test
FROM t1
但我想這樣做,當然動態和當前行ID。 我改變了查詢到以下幾點:
SELECT id,
(
SELECT SUM(number)
FROM (
SELECT number
FROM t2
WHERE u_id = p.id
ORDER BY time ASC
LIMIT 30
) AS temp
) AS test
FROM t1 p
這將提供以下錯誤:
Unknown column 'p.id' in 'where clause'
任何想法如何使它工作?
請提供樣本數據和預期輸出。 – Uriil