我有两张表如下:
表1:
ID Code Detail
1 45 Yes
2 46 No
和表2:
Code Detail1 Detail2
45 No 23
22 Yes 34
是否可以选择所有 FROM TABLE 2 ,其中detail = YES,其中TABLE 1包含Code并且是?
即。查询结果应为:
Code Detail Detail
45 No 23
22 Yes 34
感谢。
答案 0 :(得分:3)
SQL小提琴:http://sqlfiddle.com/#!2/6f583/9
SELECT
Table2.Code,
Table2.Detail1,
Table2.Detail2
FROM
Table1,
Table2
WHERE
Table1.Detail = "Yes" OR
(Table2.Detail1 = "Yes" AND
Table1.Code = Table2.Code)
虽然我的上述查询似乎为您提供了示例的正确结果集,但我认为以下查询更符合您的条件:
SQL小提琴:http://sqlfiddle.com/#!2/6f583/12
SELECT
Table2.Code,
Table2.Detail1,
Table2.Detail2
FROM
Table1,
Table2
WHERE
(Table1.Code = Table2.Code AND
Table1.Detail = "Yes") OR
Table2.Detail1 = "Yes"
GROUP BY
Table1.ID
答案 1 :(得分:1)
我没有很好地关注你的问题,但这会创建你想要的结果表。
select table2.* from table2 left join table1 on table2.code = table1.code where table2.Detail1 = 'Yes' or table1.Detail = 'Yes'