Linux中MySQL数据库的使用④-----常用查询语句、常用函数

一、常用的查询语句

1.SELECT:字段表达式

select既可以做查询,也可以做输出

select rand();  -- 输出随机数
select unix_timestamp();  -- 显示Unix时间戳
select id, name from student;
2.FROM子句

语法:select 字段 from 表名

FROM后面是数据源,数据源可以写多个,数据源一般是表名,也可以是其他查询的结果

select student.name, score.math from student, score;
3.WHERE子句:按指定条件过滤

语法:select 字段 from 表名 where 条件;
WHERE是做条件查询,只返回结果为True的数据

#将student表中city=陕西的name值查找出来
select name fron student where city='陕西';

空值判断:is null | is not null

select name from student where description is null;
select name from student where description is not null;

范围判断:between …and … | not between … and …

#查询score表中math值位于60~70之间的id和math
select id, math from score where math between 60 and 70;

#查询score表中math值不在60~70之间的id和math
select id, math from score where math not between 60 and 70;

#查询score表中math值大于等于80并且english值小于等于60的数据
select * from score where math>=80 and english<=60;
4.HAVING

HAVING和WHERE功能类似,都可以用来实现条件查询,很多情况下可以用where或者having,甚至可以混合使用。

select name, birthday from student where birthday > '1995-1-1';
select name, birthday from student having birthday > '1995-1-1';
select * from student where id>=3 and city='西安';
select * from student having id>=3 and city='西安';
select * from student where id>=3 having city='西安';

where和having的区别:
只能用是where的情况

select name, birthday from student where id > 2;

#报错。having的条件查询,只能包含在前面的搜索结果里
select name, birthday from student having id >2;

只能使用having的情况

select name as n, birthday as b, id as i from student having i > 2;

#报错。where的条件查询只识别存在的字段
select name as n, birthday as b, id as i from student where i > 2;

having后面可以跟聚合函数,where不行。

select city, min(birthday) from student group by city having min(birthday)>='1996';
5.GROUP BY:分组查询

按照某一字段进行分组,会把该字段中值相同的归为一组,将查询结果分类显示。
如果有where要放在where的后面
语法:select 字段 from 表名 group by 分组字段;

select sex, count(id) from student group by sex;

#在group将需要的结果通过聚合函数拼接
select sex, group_concat(name) from student group by sex;
6.ORDER BY:按字段排序

ORDER BY主要作用是排序
ORDER BY写在GROUP BY后面,如果有having也要写在having后面
语法:select 字段 from 表名 order by 排序字段 asc|desc;
升序asc 降序desc 默认asc

select * from student order by age;
select * from student order by age desc;
7.LIMIT:限制取出数量
#从第1行到m行
select 字段 from 表名 limit m;
#从第m行开始,往下取n行
select 字段 from 表名 limit m, n;
#跳过前n行,取后面的m行
select 字段 from 表名 limit m offset n;  
8.DISTINCT:去重
select distinct city from student;

二、函数

聚合函数
NameDescription
AVG()返回平均值
COUNT()计数
GROUP_CONCAT()返回连接的字符串
MAX()返回最大值
MIN()返回最小值
SUM()返回总和
数值计算类函数
NAMEDescription
ABS(x)返回x的绝对值
CEIL(x)返回大于x的最大整数值
FLOOR(x)返回小于x的最大整数值
MOD(x, y)返回x/y的模
RAND()返回0到1内的随机值
ROUND(x, y)返回参数x的四舍五入的有y位小数的值
TRUNCATE(x, y)返回数字x截断为y位小数的结果
日期时间相关
NAMEDescription
NOW()返回现在的日期时间
DATEDIFF(x, y)返回起始时间x和结束时间y之间的天数
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天意不可违.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值