Mysql SQL:条件测试
我想在两个计数器上测试2条件。示例下面:Mysql SQL:条件测试,mysql,Mysql,我想在两个计数器上测试2条件。示例下面: id_client | zipcode | timestamp | UC | MISS --------------------------------------------------|--------|------ amar | 11111 | 2016-09-28 20:05:03.001 | 15 | 0 akbar | 11111 | 2016-0
id_client | zipcode | timestamp | UC | MISS
--------------------------------------------------|--------|------
amar | 11111 | 2016-09-28 20:05:03.001 | 15 | 0
akbar | 11111 | 2016-09-28 20:05:03.001 | 50 | 1
antony | 11111 | 2016-09-28 20:07:03.001 | 110 | 0
amar | 11111 | 2016-09-28 20:08:03.001 | 5 | 1
select
date (CollectionTime) as dates,
id_client
case when UC <=15 THEN 'TB'
UC < 15 THEN 'TB'
UC > 15 AND UC <=40 THEN 'B'
UC >40 AND UC <=80 THEN 'M'
UC > 80 THEN 'TM'
ELSE 'NULL' END AS tranche_UC
case when MISS = O THEN 'BONNE'
MISS =1 THEN 'Mauvaise'
ELSE 'NULL' END AS note
from cm_stat
group by id_client
order by dates;
您的查询表明,您希望最后一列为
bonne
或mauvais
,而不是1或0,如示例所示。我和博恩和莫维斯一起去
首先,您有一些语法错误:
select
date (CollectionTime) as dates,
id_client, -- missing comma
case
when UC <=15 THEN 'TB'
when UC < 15 THEN 'TB' -- needed WHEN (use before every condition)
when UC > 15 AND UC <=40 THEN 'B' -- needed WHEN
when UC >40 AND UC <=80 THEN 'M' -- needed WHEN
when UC > 80 THEN 'TM' -- needed WHEN
ELSE 'NULL' END AS tranche_UC
case
when MISS = O THEN 'BONNE'
when MISS =1 THEN 'Mauvaise' -- needed WHEN
ELSE 'NULL' END AS note
from cm_stat
group by id_client
order by dates;
id\u客户端后缺少逗号
案例中的每种情况
都需要WHEN
select
date (CollectionTime) as dates,
id_client, -- missing comma
case
when UC <=15 THEN 'TB'
when UC < 15 THEN 'TB' -- needed WHEN (use before every condition)
when UC > 15 AND UC <=40 THEN 'B' -- needed WHEN
when UC >40 AND UC <=80 THEN 'M' -- needed WHEN
when UC > 80 THEN 'TM' -- needed WHEN
ELSE 'NULL' END AS tranche_UC
case
when MISS = O THEN 'BONNE'
when MISS =1 THEN 'Mauvaise' -- needed WHEN
ELSE 'NULL' END AS note
from cm_stat
group by id_client
order by dates;
您的查询表明,您希望最后一列为
bonne
或mauvais
,而不是1或0,如示例所示。我和博恩和莫维斯一起去
首先,您有一些语法错误:
select
date (CollectionTime) as dates,
id_client, -- missing comma
case
when UC <=15 THEN 'TB'
when UC < 15 THEN 'TB' -- needed WHEN (use before every condition)
when UC > 15 AND UC <=40 THEN 'B' -- needed WHEN
when UC >40 AND UC <=80 THEN 'M' -- needed WHEN
when UC > 80 THEN 'TM' -- needed WHEN
ELSE 'NULL' END AS tranche_UC
case
when MISS = O THEN 'BONNE'
when MISS =1 THEN 'Mauvaise' -- needed WHEN
ELSE 'NULL' END AS note
from cm_stat
group by id_client
order by dates;
id\u客户端后缺少逗号
案例中的每种情况
都需要WHEN
select
date (CollectionTime) as dates,
id_client, -- missing comma
case
when UC <=15 THEN 'TB'
when UC < 15 THEN 'TB' -- needed WHEN (use before every condition)
when UC > 15 AND UC <=40 THEN 'B' -- needed WHEN
when UC >40 AND UC <=80 THEN 'M' -- needed WHEN
when UC > 80 THEN 'TM' -- needed WHEN
ELSE 'NULL' END AS tranche_UC
case
when MISS = O THEN 'BONNE'
when MISS =1 THEN 'Mauvaise' -- needed WHEN
ELSE 'NULL' END AS note
from cm_stat
group by id_client
order by dates;