这是我的表数据….
id days
-----------
1 10
20
3 30
4 25
5 5
我想要结果
id days resultcolumn
1 10 less than 10
2 20 less than 20
3 30 less than 30
4 25 less than 30
5 5 less than 10
我用过查询
SELECT id,days,CASE days
WHEN days<=10 THEN 'less than 10'
WHEN days<=20 THEN 'less than 20'
WHEN days<=30 THEN 'less than 30'
END AS 'days2'
FROM calender
但我得到了结果数据
id days resultcolumn
1 10 {null}
2 20 {null}
3 30 {null}
4 25 {null}
5 5 {null}
最佳答案
你可以不用CASE …… WHEN构造.只需使用简单的数学.
SELECT *,CONCAT('less than ',CEIL(`days`/10)*10) AS `resultcolumn`
FROM `Table1`
这可以防止Branch predication失败.因此它将是faster for large data set.