這兩個查詢輸出不同的結果,
我有問題AND m.status = $1 ...
每個條件跟着左連接表或移動到最後一部分,那就不同了吧?選擇左連接表使用條件
查詢1
SELECT count(mua.*)
AS total_row_count
FROM media_user_action mua
LEFT JOIN media m ON m.id = mua.media_id
AND m.status = $1
LEFT JOIN gallery_media gm ON gm.id = mua.media_id
LEFT JOIN gallery g ON g.id = gm.gallery_id
AND g.status = $1
LEFT JOIN "user" mcbu ON mcbu.id = m.create_by_user_id
AND mcbu.status = $1
LEFT JOIN "user" gcbu ON gcbu.id = g.create_by_user_id
AND gcbu.status = $1
WHERE mua.user_id = $2
查詢2
SELECT count(mua.*)
AS total_row_count
FROM media_user_action mua
LEFT JOIN media m ON m.id = mua.media_id
LEFT JOIN gallery_media gm ON gm.id = mua.media_id
LEFT JOIN gallery g ON g.id = gm.gallery_id
LEFT JOIN "user" mcbu ON mcbu.id = m.create_by_user_id
LEFT JOIN "user" gcbu ON gcbu.id = g.create_by_user_id
WHERE
m.status = $1
AND g.status = $1
AND mcbu.status = $1
AND gcbu.status = $1
AND mua.user_id = $2
UPDATE
下面的答案
,如果我想確保返回結果,都必須與基礎/左連接表狀態都等於$ 1,(連接表recode可能爲空),所以我必須添加AND x.status ..
按照連接表,對吧?
您是在問「您應該使用哪兩個?」?因爲答案取決於你的意圖。 – Kritner
感謝您的回覆,我想選擇每個相關的表,所有狀態都等於$ 1和mua.user_id = $ 2 – user1575921