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

mysql数据库sql查询语句:多条件判断,第1张

1、创建测试表,

create table test_person(id int, RMB int)

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)

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

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

在SQL中使用if语句可以直接判断,也可以使用case语句,对小型处理进行判断。

如果判断库中是否存在某表,如果存在则删除。

if exists(select * from sysobjects where name = 'tablename')

begin

drop table tablename

end

等等等


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10031999.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存