简体   繁体   English

从第一个表中选择任何内容

[英]select nothing from first table

Quick question regarding sql syntax If I have 3 tables (hereby reffered to 1,2,3) and want to select everything from table 2,3 tables depending on if an id is present in table 1, how do I do that, IE "select nothing" from table 1? 有关sql语法的快速问题如果我有3个表(因此折为1,2,3),并想根据表1中是否存在ID从table 2,3表中选择所有内容,我该怎么做,即IE“从表1中什么都不选? As for now I select everything from table 1 . 现在,我从table 1选择所有内容。

SELECT * FROM [Content] pc, [test] Dc, 
[Swg] Swg where pc.Id=Dc.Id and pc.Id=Swg.Id  order by pc.Id

If I understand you correctly, you want to select everything from tables test and SWG, even if there is no match in table Content? 如果我对您的理解正确,即使表格内容不匹配,您还是要从表格测试和SWG中选择所有内容。

If so, a RIGHT join would do the trick: 如果是这样,那么进行RIGHT联接将达到目的:

SELECT
  *
FROM
   Content
      RIGHT JOIN test ON content.ID = test.ID
      RIGHT JOIN SWG ON Content.ID = SWG.ID

If you're looking for everything from test and SWG but only if there is a matching ID in CONTENT, then this should work: 如果您要查找测试和SWG中的所有内容,但仅当CONTENT中有匹配的ID时,这才有效:

SELECT 
    test.*
    , SWG.*
FROM
   Content
      JOIN test ON content.ID = test.ID
      JOIN SWG ON Content.ID = SWG.ID

So you just don't want to see the columns from table 1? 因此,您只是不想看到表1中的列?

SELECT Dc.*, Swg.*
FROM [Content] pc, [test] Dc, 
[Swg] Swg where pc.Id=Dc.Id and pc.Id=Swg.Id  order by pc.Id

That assumed pc = table 1, dc = table 2 and swg = table 3 假设pc =表1,dc =表2,swg =表3

eg if table 1 is Content (pc alias in your query): 例如,如果表1是Content(查询中的pc别名):

SELECT Dc.*, swg.*
FROM ....

So, you have to specify the table aliases you want to select from. 因此,您必须指定要从中选择的表别名。 * alone will select all columns from all referenced tables *仅会从所有引用的表中选择所有列

You may Tried This 你可能会试过

SELECT pc.* ,Dc.* ,Swg.* FROM [Content] pc, [test] Dc, [Swg] Swg where (pc.Id=Dc.Id or pc.Id=Swg.Id) order by pc.Id 从[Content] pc,[test] Dc,[Swg] Swg中选择pc。*,Dc。*,Swg。*,其中(pc.Id = Dc.Id或pc.Id = Swg.Id)按pc.Id排序

Select * from 
T1 left join T2 on T1.id=T2.id 
Where T1.id in (select id from T3 where...etc.)

This way, you will get everything from T1 , and matching T2 values if T1 values matches values from T3 without getting this T3 table values 这样,您将从T1中获取所有信息,如果T1值与T3中的值匹配,则匹配T2中的值,而无需获取此T3表值

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

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