我需要一条SQL Server Select语句在表之间执行左联接,以便根据右表中的特定值过滤结果.
下面是一个例子: 左表将保存 colored颜色 值.右边的表格将包含左边表格的 colored颜色 组合的行.
tblObject /*this is the left table */
objID objName
1 Violet
2 Rose
3 Grass
4 Sky
5 Magenta
tblColorCombination /*this is the right table */
ccID objID Color
1 1 Red
2 1 Blue
3 2 Red
4 3 Green
5 4 Blue
6 5 Blue
7 5 Red
SELECT应返回tblColorCombination中具有‘Red’、‘Blue’组合的tblObject行
ccID obj ID objName Color
1 1 Violet Red
2 1 Violet Blue
6 5 Magenta Blue
7 5 Magenta Red
我try 了这个查询,但未能过滤出正确表中的Rose和Sky
SELECT T1.ccID, T0.objID, T0.objName, T1.Color
FROM tblObject T0
LEFT JOIN tblColorCombination T1 ON T1.objID = T0.objID AND T1.Color IN ('Red', 'Blue')