2012-05-04 45 views
0

我有一個名爲tblClients與下一個字段的表:idClient,名稱,信用,債務。Mysql結果行到字符串,UNION

Create table tblClients(idClient int,name varchar(20), credit int, debt int); 
insert into tblClients values(1,'Guillermo',1000,0),(2,'Jess',5000,2000); 

我要歸類的債務類型像

debt = 0 NOT    DEBTOR 
debt between 0 AND 1000  LOW DEBTOR 
debt between 1001 AND 2000 MEDIUM DEBTOR. 

如果我讓這個查詢我得到的債務人類型。

SELECT name AS 'Client',debt AS 'Debtor Type' FROM tblClients WHERE debt = 0; 
SELECT name AS 'Client',debt AS 'Debtor Type' FROM tblClients WHERE debt BETWEEN 1 AND 1000; 

我怎樣才能把標籤DEBTORLOW DEBTORMEDIUM DEBTOR在列,而不是債務的債務是多少?

回答

2

試試這個:

SELECT name AS Client, 
CASE 
    WHEN debt = 0 THEN 'Not Debtor' 
    WHEN debt BETWEEN 1 AND 1000 THEN 'Low Debtor' 
    ELSE 'Medium Debtor' 
END AS debt 
FROM tblClients 
+0

IT運行完美!!!!非常感謝!!! – unixeO

+0

你應該接受這個答案,如果它對你有幫助,就可以加註。 –