简体   繁体   English

SQL Server 2005中的查询

[英]query in sql server 2005

there are two tables ...know i need 有两张桌子...知道我需要

1st condition: all the records in table 第一个条件:表中的所有记录

2 nd condition: 第二条件:

In table2 i need only records which have data 在表2中,我只需要有数据的记录

...i want one query for the aove two conditions... 我想要一个查询同时满足两个条件

SELECT
  *
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK

This will return all rows in table1 that have at least one corresponding row in table2 这将返回表1中所有至少具有表2中相应行的行

But if you want all rows from t1 no matter what then this may be what you want 但是,如果您想要从t1开始的所有行,无论如何,那可能就是您想要的

SELECT
  *
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK

Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. 最后,由于我不知道其结构,也许table1和table2具有类似的结构。 If this is true perhaps you may want a union of the two 如果这是真的,那么您可能想要两者的结合

SELECT
  *
FROM Table1 t1
UNION ALL
SELECT
  *
FROM Table2 t2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM