更新:這已解決,我正在發出語法錯誤。Sql用兩個列左加入過濾器
我可以連接並過濾左連接中的兩列嗎?例如:
tbl_people
id food side value
a pizza fries 10
b pizza shake 2
c burger fries 3
tbl_sides
food side
pizza fries
burger fries
然後利用SQL:
SELECT
id, food, side, value
FROM
tbl_people AS people
LEFT JOIN
tbl_sides AS sides ON sides.food = people.food
AND sides.side = people.side
我可以添加一個標誌,以便我能確定的食品對是否是在加入或如果它是NULL?我不想內部加入,因爲我需要計算每個人的總食量/人數,並且還要匹配每個人的食物/人對。我試過:
SELECT
id, food, side, value,
CASE WHEN
side.side IS NOT NULL
AND side.food IS NOT NULL
THEN 1
ELSE 0
END AS match_flag
FROM
tbl_people AS people
LEFT JOIN
tbl_sides AS sides ON sides.food = people.food
AND sides.side = people.side
但它不工作。基本上我只需要標記何時未加入連接但我遇到問題。
哦,你說的沒錯,我是過於複雜,我很抱歉。 –