更新:这已解决,我犯了一个语法错误。
我可以在左连接中连接和过滤两列吗?例如:
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
但它不起作用。基本上,我只需要在未应用联接但遇到问题时进行标记。