0

一旦我需要你的帮助,

我的数据库中有三个表。

1>婚礼

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

2>选择站点

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>网站

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

我想要以下输出:

 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     

wswebSites的别名,是表的别名siteSelect

如果 wedID 不匹配,则表中的所有数据webSite和仅显示siteSelect其中的数据s.siteStatusYes

我尝试以下 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

但输出是这样的:

 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
4

2 回答 2

2

演示 SQL 小提琴

检查演示添加一些值并运行此查询

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

让我知道是否有任何问题

于 2012-08-24T07:12:31.517 回答
0

尝试这个

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
于 2012-08-24T06:42:26.030 回答