0

我有一个名为 tblClients 的表,其中包含以下字段:idClient、姓名、信用、债务。

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 DEBTOR或者MEDIUM DEBTOR 在债务列而不是债务编号?

4

1 回答 1

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 
于 2012-05-04T19:42:58.673 回答