简体   繁体   English

根据另一个表中的记录选择一个表中的记录

[英]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.

相关问题 Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 根据表B记录从一个表中选择记录 - select records from one table based on table B records SQL Server-基于另一个表中的记录的SELECT记录 - SQL Server - SELECT records based on the records from another table 根据另一个表中的条件选择表中的记录? - Select records in on table based on conditions from another table? 从基于另一个表的表中选择记录数Oracle - Select from a table based on another table Number of records Oracle SQLite - 如何从一个表中的 select 记录不在另一表中 - SQLite - How to select records from one table that are not in another table 根据另一个表的“相似”值从一个表中检索记录 - Retrieving records from one table based in 'similar' values of another table Select 表记录来自另一个表 - Select table records from another table MYSQL根据另一个表的几个不同值从一个表中选择5条记录 - MYSQL Select 5 records from one table based on several different values from another 根据两个表选择记录,其中一个表中的一列使用SQL从另一个表列的记录开始 - Select records based on two tables where one column in one table starts with the records from another tables column using SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM