我試圖計數tc.personnel_id的結果,如果有超過1個結果,那麼輸出是1.然後最後,它會得到總和。但是當我執行這段代碼時,我得到了「無效的組功能使用」錯誤。任何幫助將不勝感激。MySQL獲取和無效的組功能使用
SELECT tv.id,
tv.vesselName,
SUM(IF(tpi.returningCrew = 0, 1,
IF(
COUNT(tc.personnel_id) > 1, 1, 0)
)
) AS ex_crew,
SUM(IF(tpi.returningCrew = 1, 1, 0)) AS new_hire
FROM tbl_vessels AS tv
LEFT JOIN tbl_personnel_info AS tpi
ON tv.id = tpi.lastJoinedVsl
LEFT JOIN tbl_contracts AS tc
ON tpi.id = tc.personnel_id
LEFT JOIN tbl_management AS tm
ON tm.id = tv.management
WHERE tpi.emp_status = 'ON-BOARD'
AND tc.status = 'ACTIVE'
AND tv.trade_route = 'WORLD WIDE'
AND tm.management = 'TOKYO'
GROUP BY vesselName;
不知道在MySQL這樣的事情,而是你有兩個非'SELECT'中的聚合字段,而'GROUP BY'只提到一個。 – yeputons
你不能嵌套像SUM(COUNT(...))' –