1
有無查詢,如:如何在表中選擇對每列不同的數據
declare @type int = 1
select cf.id, cf.name
from dbo.tbl1 cf
inner join dbo.tbl2 ct on cf.id = ct.field_id and ct.[type_id] = @type
的迴應是:
ID NAME
1 number
2 city
4 location
5 username
我要加入響應的一個領域,我必須讓從選擇的每個名稱 類似:
when name = number then select number from myTbl
when name = city then select name from region_tbl
when name = location then select name from location_tbl
when name = username then select user from user_tbl
和響應必須像:
ID NAME Field
1 number 1
2 city Paris
4 location some_location
5 username admin
是否有可能做這樣的事情?
或者我可以在表中添加一個多列和響應將是這樣的:
ID NAME command
1 number select number from myTbl
2 city select name from region_tbl
4 location select name from location_tbl
5 username select user from user_tbl
,並動態執行命令,反應必須是這樣的:
ID NAME command
1 number 1
2 city Paris
4 location some_location
5 username admin
您可以使用'CASE和子查詢'並獲得所需的輸出 – 2014-12-05 08:39:25