简体   繁体   English

如何使用连接查询从两个以上的表中获取数据,而无需外键和主键

[英]How to use join query to fetch data from more than two tables without foreign key and primary key

Once I need your help, 一旦我需要你的帮助,

I have three table in my database. 我的数据库中有三个表。

1> wedding 1> 婚礼

Column Name    Data Type
-------------------------------------
wedID          Int (primary key)
wedName        varchar (50)

2> selectSite 2> selectSite

Column Name   Data Type
-----------------------------------
wedID         Int (without foreign key and primary key with null allow)
siteID        Int (without foreign key and primary key with null allow)
siteStatus    varchar(50)

3> webSite 3> webSite

Column Name   Data Type
--------------------------------      
siteID        Int (Primary Key) 
siteName      varchar(50)
siteFile      varchar(MAX)

I want the following output: 我想要以下输出:

 ws.siteID     ws.siteName     ws.siteFile    s.wedID   s.sitestatus
 ----------------------------------------------------------------------------
 1              Show           show.jpeg      6         Yes
 2              My View        my view.png    5     
 3              Dream          dream.jpeg     3     

ws is alias of webSite table, s is alias of siteSelect table. wswebSites别名, ssiteSelect表的别名。

All data from webSite table and only data from siteSelect where s.siteStatus do not display Yes if wedID not match 来自webSite表的所​​有数据以及来自siteSelect数据,其中s.siteStatus不显示Yes如果wedID不匹配

I try following sql query 我尝试下面的SQL查询

select 
    DISTINCT(ws.siteID), s.wedID, ws.siteName, 
    ws.siteFile, s.siteStatus 
from wedding wd, webSite ws 
left outer join siteSelect s on ws.siteID = s.siteID 
where wd.wedID = @wedID

but output is like this: 但是输出是这样的:

 ws.siteID     ws.siteName     ws.siteFile    s.wedID   s.sitestatus
 -----------------------------------------------------------------------
 1              Show           show.jpeg      6         Yes
 2              My View        my view.png    5         Yes
 3              Dream          dream.jpeg     3         Yes

DEMO SQL FIDDLE DEMO SQL FIDDLE

Check the demo add some values as you having and run this query 检查演示添加一些值并运行此查询

select 
   ws.siteId, ws.siteName, ws.siteFile, ss.wedID,
   (case 
      when ss.wedID = 1 then ss.siteStatus else '' end) as siteStatus
from 
   website ws 
join 
   selectsite ss on ss.siteID=ws.siteID

let me knw if any prob 如果有任何疑问,请告诉我

try this 尝试这个

select 
    DISTINCT(ws.siteID), s.wedID, ws.siteName, ws.siteFile, 
    (case 
        when s.wedID = @wedID 
        then s.siteStatus 
        else '' end) as siteStatus, wd.wedName
from webSite ws  
left join selectSite s on ws.siteID = s.siteID  
Left Join wedding wd on wd.wedID = s.wedID

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

相关问题 如何在两个以上的表上执行Sql Join查询? - How to perform Sql Join Query on more than two tables? 实体框架从两个表关系不同的表中选择主/外键场景 - Entity framework select from two tables with different table relationship primary/foreign key scenarios 引用同一个主键的两个外键 - Two foreign keys referencing the same primary key 插入到两个表中,其中一个表具有自动增量主键,并且同一键用作另一表中的外键 - Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table 用两个外键联接两个表 - join two table with two foreign Key 如何在没有主键的情况下使用实体框架核心调用表 - How call tables without primary key with entity framework core 同一张表有多个外键? - More than one Foreign Key to the same table? 在不知道主键的情况下插入多个表 - insert into multiple tables without knowing the primary key 从两个外键引用到两个不同的表制作复合键时出现错误 - ihave an error with making composite key from two foreign key references to two different tables 如何在ASP.NET图表控件中使用外键表中的值 - How to use values from foreign key tables in asp.net chart control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM