我有一个名为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 DEBTORMEDIUM DEBTOR放在债务栏中而不是债务编号上?

最佳答案

尝试这个:

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

关于mysql - MySQL结果行到字符串,UNION,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10455073/

10-10 23:54