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

发布网友

我来回答

7个回答

懂视网

1.查询数据

   select  属性列表

            from  表名和视图列表

            [where  条件表达式1]

            [group by 属性名1  [having 条件表达式2] [with rollup最后加一条记录是上面记录的总和]]  //按照属性名1指定的字段分组,有having则要满足条件表达式2

     [order  by 属性名2  [asc|desc]]

      where查询条件:

      比较          =、<、<=、 >、>=、!=、<>、!>、!<

      指定范围     between and、not between and

      指定集合     in、not in

      匹配字符     like、not like <%:代表任意长度字符串,_:代表单个字符>

      是否为空值  is null、is not null

eg:select * from test0 where id in (1001,1004,1005);

     select * from test0 where id between 1002 and 1005;

   select * from test0 where id < 1004 and name like ‘h_h_‘; //两个条件均满足

     select * from test0 where id < 1004 or name like ‘h%‘; // 满足其中一个便可

   select distinct name from test0;   //查询结果不重复 

   select id,group_concat(name) from test0 group by name;//将name分组中指定字段值都显示出来

   select name,count(name) from test0 group by name;//将name分组的每一组记录数统计出来

   select name,count(name) from test0 group by name having count(name) > 1; //显示记录数大于1的

     having表达式是作用于分组后的记录,而where作用于表或者视图。

   select name,count(name) from test0 group by name with rollup;

     select * from test0 limit 3; //只显示3条记录

     select * from test0 limit 1,3; //从第2条记录起显示3条记录

     

MySQL基本操作-SQL查询语句

标签:

热心网友

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中使用 case when then可以达到多条件判断的目的

例子
表格 每个国家的人口数据
国家(country) 人口(population)
中国 600
美国 100
加拿大 100
英国 200
法国 300
日本 250
德国 200
墨西哥 50
印度 250
根据这个国家人口数据,统计亚洲和北美洲的人口数量。
sql语句
SELECT SUM(population),
CASE country
WHEN '中国' THEN '亚洲'
WHEN '印度' THEN '亚洲'
WHEN '日本' THEN '亚洲'
WHEN '美国' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END
FROM Table_A
GROUP BY CASE country
WHEN '中国' THEN '亚洲'
WHEN '印度' THEN '亚洲'
WHEN '日本' THEN '亚洲'
WHEN '美国' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END;

结果
洲 人口
亚洲 1100
北美洲 250
其他 700

热心网友

select
case when rmb>100 and rmb <=200 then 'VIP1' else 'VIP2' end 级别,
sum(case when rmb>100 and rmb <=200 then 1 else 0 end) 人数
from 表名
group by case when rmb>100 and rmb <=200 then 'VIP1' else 'VIP2' end

热心网友

select t.vip,count(*) from(select case when rmb>100 and rmb < 200 then 'vip1' when rmb>200 then 'vip2' end 'vip',rmb from test) t group by t.vip追问我的case when then 怎么不能用?

追答不应该啊 case when then when then....(else) end 注意后面有个end 我已经运行成功了

热心网友

select if(rmb>100,if(rmb>200,2,1),0) as tt,count(1) as cnt from yourtable group by tt;

热心网友

select distinct (select count(*) from table where 账户金额>100 and 账户金额<200) as vip1,
(select count(*) from table where 账户金额>200) as vip2,
form table

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com