关于mysql 指定包含条件查询

因为项目需求后期又加了一个功能。
打个比方;
有张表 student
字段有
id name age
1 张三 25
2 张四 27
3 李大 30
4 赵四 20
5 陈大 24
在页面中增加了一项年龄范围查询
<li>
<label>年龄范围:</label>
<option value="0">20-25</option>
<option value="1">25-30</option>
</li>
就是说 页面传来0和1
0代表 20至25岁之间的人
1代表 25至30岁之间的人
要是用mysql写的话 这段查询如何查出来呢?
望各位大神多多指教!!

第1个回答  2019-01-24
SELECT 
            CASE WHEN (age >=20 and age <25) THEN 0
            CASE WHEN (age >=25 and age <=30) THEN 1
            ELSE -1 END  
            FROM  student

用case when

    case

    when 条件1 then 值1      

    when 条件2 then 值2      

    else 值3      

    end  

第2个回答  2019-01-24
select * from student where 1=1
<if test="age == 0">
and age between 20 and 25
</if>
<if test="age == 1">
and age between 25 and 30
</if>

上面是mybatis的写法,你可以参考下

本回答被提问者采纳
第3个回答  2019-01-23
select * from student where age between 20 and 25;

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网