2016-07-27 199 views
0

我需要使用查詢來選擇一些數據,其中需要顯示授權數據,其餘數據應該用XXXXXXXX標記,但我需要從表中獲取所有數據單個查詢。SQL查詢根據條件選擇不同的選擇列表

select empid, orgid, fname, lastname, mname, depid, date 
from employee 
where orgid in (100,200,300,400,500) 

輸出應該是這樣的:

10 100 3 "dfdf" "dfsdfd" "sdfsdfd" "sdfsdfd" "10/10/2010" 
20 200 4 "dfdf" "dfsdfd" "sdfsdfd" "sdfsdfd" "10/10/2010" 
30 300 5 "dfdf" "dfsdfd" "sdfsdfd" "sdfsdfd" "10/10/2010" 
40 400 6 "dfdf" "dfsdfd" "sdfsdfd" "sdfsdfd" "10/10/2010" 
50 500 7 "dfdf" "dfsdfd" "sdfsdfd" "sdfsdfd" "10/10/2010" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
XX XXXX X "xxxx" "XXXXXX" "XXXXXXX" "XXXXXX" "XXXXXXXXXX" 
+0

MySQL或SQL服務器? – Madhivanan

+0

我在你想要的輸出中計數8列,但你的'SELECT'列表只有7列。 –

+0

幫我理解你在這裏試圖做什麼......所以,你想要返回表*的全部內容,但是用'X'來檢查所有不符合你的'WHERE'要求的東西? – Siyual

回答

0
select empid, orgid, fname, lastname, mname, depid, date 
from employee 
where orgid in (100,200,300,400,500) 
UNION ALL 
select 0, 0, 'XXXX', 'XXXX', 'XXXX', 0, '1900-01-01' 
from employee 
where orgid NOT in (100,200,300,400,500) 

上述查詢應該做的工作。但是,如果您在結果集中具有'xx ...'的特殊性,則需要將結果集轉換爲varchar(nn)。

下面是一個例子(當然,你需要更換的字段和表名)

SELECT CAST(CourseId AS VARCHAR(20)), Title, CAST(Credits AS VARCHAR(20)), 
CAST(DepartmentID AS VARCHAR(20)), CONVERT(VARCHAR(20), GetDate(),101) 
FROM Course 
WHERE DepartmentID IN (1, 2) 
UNION ALL 
SELECT 'xx', 'XXXX', 'xxx', 'xxxx', 'XXXXXX' FROM Course 
WHERE DepartmentID NOT IN (1, 2)