赞
踩
- select *
- from student
- group by `sex`
- select sex, count(*)
- from student
- GROUP BY sex
- select sex, count(*)
- from student
- GROUP BY sex
- with rollup
因为select 在sql执行中,在where和having之后,所以你给别名age,在where或者having进行条件筛选时没有作用,创建临时表,可以有效解决这一问题
- (select
- `name`,
- `sex`,
- `city`,
- money,
- year(NOW()) - year(birthday) as age
- from student
- )as temp
这里用了 临时表, group by 分组, having 对分组后的结果进行进一步筛选
- select city as 城市,COUNT(*) as 计数
- from
- (select
- `name`,
- `sex`,
- `city`,
- year(NOW()) - year(birthday) as age
- from student
- )as temp
- where age < 30
- group by city
- HAVING 计数 >1
- order by () asc 正序
- order by () desc 倒序
- order by sex,city desc 会先按照sex 降序,再按照 city降序
- order by rand() 随机排序
-
- select city as 城市,COUNT(*) as 计数
- from
- (select
- `name`,
- `sex`,
- `city`,
- year(NOW()) - year(birthday) as age
- from student
- )as temp
- where age < 30
- group by city
- order by 计数 asc # 正序
- select 字段 from 表名 limit m; -- 从第 1 个到第 m 个
- select 字段 from 表名 limit m, n; -- 从第 m 个开始,往下取n 个
- select 字段 from 表名 limit m offset n; -- 跳过前 n 个, 取后面的 m 个
在模糊查询中,_ 代表的是匹配一个字符串, %代表是0~多位
- select *
- from student
- where `name` like "郭%"
SELECT ROUND(2.333,2)
- select
- CONCAT("123","abc")
-
- -- 123abc
城市有重复,看一共有哪些城市
- select
- count(DISTINCT city)
- from student
当不使用group_concat时,结果如下图,上海后的name只有一个人名字
- select city,`name`
- from student
- group by city
- select city,GROUP_CONCAT(`name`)
- from student
- group by city
返回 x/y 的模
- -- 1997-10-01
- select DATE_FORMAT(birthday,"%Y-%m-%d") FROM STUDENT
select DATE_ADD("2019-8-8",INTERVAL 1 YEAR) from student
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。