編輯:我需要在那裏減去直接和間接分鐘的總數。SELECT語句子查詢
我想要SUM M.分鐘作爲別名「dminutes」。然後,再次取M.minutes的SUM並減去具有「間接」列值的M.minutes(並給它「inminutes」別名)。但是,它顯示爲空,所以語法錯誤。建議?
table = tasks
column = task_type
Example:
M.minutes total = 60 minutes
M. minutes (with "direct" task_type column value) = 50 minutes (AS dminutes)
M. minutes (with "indirect" task_type column value) = 10 minutes (AS inminutes)
SQL語句:
SELECT
U.user_name,
SUM(M.minutes) as dminutes,
ROUND(SUM(M.minutes))-(SELECT (SUM(M.minutes)) from summary s WHERE ta.task_type='indirect') as inminutes
FROM summary S
JOIN users U ON U.user_id = S.user_id
JOIN tasks TA ON TA.task_id = S.task_id
JOIN minutes M ON M.minutes_id = S.minutes_id
WHERE DATE(submit_date) = curdate()
AND TIME(submit_date) BETWEEN '00:00:01' and '23:59:59'
GROUP BY U.user_name
LIMIT 0 , 30
我認爲你是正確的,在正確的軌道上!雖然,我得到一個語法錯誤..我試過調整它,但它不喜歡臨時表加入「ON iuser_id = duser_id」。錯誤:#1064 - 你的SQL語法錯誤;檢查與你的MySQL服務器版本相對應的手冊,在'ON iuser_id = duser_id'附近使用正確的語法'' – Mike
你是我的朋友,是某種巫師!哈哈它完美的工作,謝謝你! – Mike
這對我來說很瘋狂,需要多少編碼才能在Mysql中進行簡單的減法工作。不要誤解我的意思,我喜歡它,因爲我只需要做一次,然後聲明就會永遠使用,但是人......大聲笑 – Mike