发布网友 发布时间:2022-04-23 10:15
共7个回答
懂视网 时间:2022-04-30 16:03
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查询语句
标签:
热心网友 时间:2022-04-30 13:11
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
热心网友 时间:2022-04-30 14:29
在sql中使用 case when then可以达到多条件判断的目的热心网友 时间:2022-04-30 16:03
select
热心网友 时间:2022-04-30 17:55
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 我已经运行成功了
热心网友 时间:2022-04-30 20:03
select if(rmb>100,if(rmb>200,2,1),0) as tt,count(1) as cnt from yourtable group by tt;热心网友 时间:2022-04-30 22:27
select distinct (select count(*) from table where 账户金额>100 and 账户金额<200) as vip1,