mysql数据库sql查询语句:多条件判断

2025-04-07 18:57:03

1、创建测试表,

create table test_person(id int, RMB int);

mysql数据库sql查询语句:多条件判断

2、插入测试数据

insert into test_person values(1,180);

insert into test_person values(2,170);

insert into test_person values(3,290);

insert into test_person values(4,160);

insert into test_person values(5,299);

insert into test_person values(6,266);

insert into test_person values(7,155);

mysql数据库sql查询语句:多条件判断

3、查询表中所有记录,select t.* from test_person t,

mysql数据库sql查询语句:多条件判断

4、编写sql,汇总每个vip类型的用户数,

select vip_type, count(distinct id)

from (select case when RMB>100 and RMB<200 then 'VIP1' when RMB>200 then 'VIP2' end as vip_type, id

from test_person) t

group by vip_type

mysql数据库sql查询语句:多条件判断

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢