我有一个具有id字段的SQL查询 - 将其视为外键。我需要根据这个id字段的值做出一个desicion:
声明如下:
Select a.ID, a.SN, a.User_Ident,
(select b.first_name from b where b.ident = a.User_Ident) as 'First Name',
(select b.last_name from b where b.ident = a.User_Ident) as 'Last Name',
from a
where ...
我想要完成的是这样的事情:
Select a.ID, a.SN, a.User_Ident,
when a.User_Ident > 3100 then
(select b.first_name from b where b.ident = a.User_Ident) as 'First Name',
(select b.last_name from b where b.ident = a.User_Ident) as 'Last Name'
else
(select c.name from c where c.ident = a.User_Ident) as 'Name'
from a
where ....
这可能吗?
更新:您的回答建议我使用左连接。我的查询已包含几个左外连接,所以我不知道这将如何工作。这是完整的查询:
select
A.Ident,
A.Serial_Number,
A.Category_Ident,
C.Description as Category,
A.Purchase_Order,
A.Manufacturer_Ident,
M.Description as Manufacturer,
A.Hardware_Model,
A.Processor_Quantity,
A.Processor_Speed_Hertz,
A.Memory_Installed_Bytes,
A.Memory_Maximum_Bytes,
A.Memory_Slots_Used,
A.Memory_Slots_Total,
A.Storage_Capacity_Bytes,
A.Video_Memory_Bytes,
A.Screen_Size_Diagonal_Inches,
A.Software_Ident,
S.Software_Title,
A.Account_Ident,
T.Description as Account,
A.User_Ident,
(select Q.dbo.P.user_name from Q.dbo.P where Q.dbo.P.ident = A.User_Ident) as 'User Name',
(select Q.dbo.P.first_name from Q.dbo.P where Q.dbo.P.ident = A.User_Ident) as 'First Name',
(select Q.dbo.P.last_name from Q.dbo.P where Q.dbo.P.ident = A.User_Ident) as 'Last Name',
(select Q.dbo.R.description from Q.dbo.R where Q.dbo.R.ident = (select Q.dbo.P.rank from Q.dbo.P where Q.dbo.P.ident = A.User_Ident)) as 'Rank',
(select Q.dbo.P.phone from Q.dbo.P where Q.dbo.P.ident = A.User_Ident) as 'Phone',
(select Q.dbo.P.smtp_address from Q.dbo.P where Q.dbo.P.ident = A.User_Ident) as 'Email',
(select Q.dbo.O.description from Q.dbo.O where Q.dbo.O.ident = (select Q.dbo.P.organization_ident from Q.dbo.P where Q.dbo.P.ident = A.User_Ident)) as 'Organization',
(select Q.dbo.L.description from Q.dbo.L where Q.dbo.L.ident = (select Q.dbo.P.location_ident from Q.dbo.P where Q.dbo.P.ident = A.User_Ident)) as 'Location',
A.Disposition_Ident,
D.Description as Disposition,
A.Notes,
A.Updated,
A.UpdatedBy,
A.Label,
A.Scanned,
S.Licensed
FROM Assets
left outer join C on A.Category_Ident = C.Ident
left outer join M on A.Manufacturer_Ident = M.Ident
left outer join S on A.Software_Ident = S.Ident
left outer join T on A.Account_Ident = T.Ident
left outer join D on A.Disposition_Ident = D.Ident
WHERE ((T.Description like '%' + @Account + '%') or (A.Account_Ident like '%' + @Account + '%'))
order by Serial_Number
答案 0 :(得分:2)
很多方法可以给猫皮肤,但我认为这种方法值得一试,使用UNION结合2种不同条件的结果(1个查询加入b,ids为> 3100,另一个查询加入c对于ids< = 3100)。
你必须返回相同的字段(你不能按照你想要的那样),当你为b条件返回2个字段时,查看c时返回1“name”字段。因此,在此示例中,当您加入c时,它将“name”作为First Name返回,并返回一个空白的Last Name值。
Select a.ID, a.SN, a.User_Ident, b.first_name AS 'First Name', b.last_name AS 'Last Name'
FROM a
JOIN b ON a.User_Ident = b.ident
WHERE (a.User_Ident > 3100)
AND (......)
UNION ALL
Select a.ID, a.SN, a.User_Ident, c.name AS 'First Name', '' AS 'Last Name'
FROM a
JOIN c ON a.User_Ident = c.ident
WHERE (a.User_Ident <= 3100)
AND (......)
答案 1 :(得分:1)
我用两个左连接和一个case语句完成了这个:
select a.field1,
case when b.lastname is not null then b.firstname else c.firstname end,
case when b.lastname is not null then b.lastname else c.larstname end
from table1 a
left join table2 b
on a.id = b.id
left join table3 c
on a.id = c.id
注意我使用的lastname在两个案例中都是空的,因为你可能不喜欢; t希望第一个名字形成一个表而最后一个名字形成另一个名字,而lastname不太可能比实际表格中的名字为空。
答案 2 :(得分:0)
CASE只能返回一列而不是一行,并且您无法将查询与其关联。无论如何,结果集必须具有静态的列数。
我认为你想要的是一个结果集,如果填写了First Name
或Last Name
名称为null
。
a.ID | a.SN | Name | First Name | Last Name
1 | # | Name | null | null
2 | # | null | John | Doe
答案 3 :(得分:0)
Union会工作(在那里击败我),或者你也可以使用内联函数。就像其他人说的那样,你必须返回相同的字段。
答案 4 :(得分:0)
HLGEM的回答略有不同
select a.field1,
case when b.lastname is not null then b.firstname else c.firstname end,
case when b.lastname is not null then b.lastname else c.larstname end
from table1 a
left join table2 b
on a.User_Ident > 3100 AND a.id = b.id
left join table3 c
on a.User_Ident <= 3100 AND a.id = c.id
答案 5 :(得分:0)
我对外部联接解决方案的看法是:
SELECT
a.id
,a.ssn
,a.user_ident
,case when a.user_ident <= 3100 then c.name else b.lastname end LastName
,case when a.user_ident <= 3100 then '' else b.firstname end FirstName
from a
left outer join b
on b.ident = a.user_ident
left outer join c
on c.ident = a.user_ident
查询不能改变返回的列数(至少在我熟悉的系统中没有),所以如果你有一个“case C”的情况并且只有Name,我会把它作为LastName返回并设置FirstName到空字符串。如果它更适合您的应用程序,则可以将其设置为NULL。