[英]Select records from one table based on records from another table
this is a simplified version of a problem I'm having, 这是我遇到的问题的简化版本,
I have two tables: 我有两个表:
Table1 has two columns (Stuff, YesNo) and
Table2 has one column (Stuff)
The records in the YesNo Column will either be 1 or 0 YesNo列中的记录将为1或0
How could I select records in Table2 where the records in Table1.YesNo = 1 我如何选择Table2中的记录,其中Table1.YesNo = 1
Many Thanks 非常感谢
SELECT Table2.*
FROM Table2
INNER JOIN Table1 ON Table1.Stuff = Table2.Stuff
WHERE Table1.YesNo = 1
If I understand you correctly, this would be your solution: 如果我对您的理解正确,那么这将是您的解决方案:
Select Stuff From Table2
Where Exists (
Select 'Y'
From Table1
Where Table1.Stuff = Table2.Stuff
And YesNo = 1
)
As I believe you'll need data from both tables and you may want to render fields unique to each table This seems like a likely response. 我相信您将需要两个表中的数据,并且您可能希望呈现每个表唯一的字段,这似乎是一种可能的响应。 However, as I don't believe STUFF accurately represents the relationship... you'll need to quantify/adjust the on a.stuff = b.stuff
so that the join includes all necessary fields. 但是,由于我不相信STUFF可以准确地表示这种关系...您需要量化/调整on a.stuff = b.stuff
以便on a.stuff = b.stuff
包括所有必要的字段。
SELECT A.Stuff, B.Stuff, B.YesNo
FROM table1 B
INNER JOIN table2 A
on A.Stuff = B.Stuff
WHERE B.YesNo = 1
SELECT T2.*
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.Stuff = T2.Stuff
WHERE T1.YesNo = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.