MySQL数据库从入门到精通 第四章内容总结

MySQL数据库从入门到精通 第四章内容总结

select相关命令

查询 指定 / 所有 / 条件 字段

select 字段1,字段2··· from 表名称;		-- 指定查询
select * from 表名称;					-- 所有查询

-- 条件查询1👇
select 字段1,字段2··· from 表名称 where 条件表达式;
	-- PS: select * from stu where gender='female';
	-- PS: select * from stu where age<=21;
    
-- 条件查询2👇	AND / OR
select 字段1,字段2··· from 表名称 where 条件表达式1 AND/OR 条件表达式2;
	-- PS: select * from stu where gender='female' AND age<=21;--AND--
	-- PS: select * from stu where gender='female' OR age<=21;--OR--
    
-- 条件查询3👇	IN / NOT IN
select 字段1,字段2··· from 表名称 where 字段名 [NOT] IN(1,2,···);
	-- PS: select s_id,s_name from stu where s_id IN(001,002); --IN--
    -- PS: select s_id,s_name from stu where s_id NOT IN(001,002); --NOT IN--
    -- PS: select * from stu where s_name IN('张三');
    
-- 条件查询4👇	IS NULL / IS NOT NULL
select 字段1,字段2··· from 表名称 where 字段名 IS [NOT] NULL;
    -- PS: select s_id,s_name from stu where s_id IS NOT NULL;
    -- PS: select s_id,s_name from stu where s_id IS NULL;
    
-- 条件查询5👇	BETWEEN AND / NOT BETWEEN AND
select 字段1,字段2··· from 表名称 where 字段名 [NOT] BETWEEN1 AND2;
	-- PS:	select s_id,s_name from stu where age BETWEEN 18 AND 20;
    
-- 条件查询6👇 LIKE
select 字段1,字段2··· from 表名称 where 字段名 [NOT] LIKE '匹配字符串';
	-- PS:	select * from where sname LIKE '张%';
    -- PS:	select * from where sname LIKE '李_';
    -- PS:	“ % ” 表示0~n个字符
    -- PS:	“ _ ” 表示一个字符
    
-- ---------------------------------------------------------------------------------
	去重的DICTINCT👇
-- -- -- PS:	以上所有命令均可以:--select DICTINCT 字段名from 表名称;--
-- -- -- PS:							增加👆DICTINCT去除重复数据--------------
-- ---------------------------------------------------------------------------------

高级查询

排序查询

select 字段1,字段2··· from 表名称 order by 字段名1 [ASC|DESC],字段2 [ASC|DESC];
	-- select * from stu order by s_name ASC,s_id DESC;
    -- ASC表示升序,DESC表示降序			-- 字段2 为 字段1 的二级排序

聚合函数


1、COUNT( ) 计算输出行数
select COUNT(*|1|列名) from 表名称;		-- 列名 = 字段名
	-- select 
 	-- COUNT()函数有3个可选参数
    COUNT(*)	-- 是返回行数,包含NULL	
    COUNT(列名)	-- 是返回特定列的值具有的行数,不包含NULL
    COUNT(1)	-- 等于COUNT(*),返回的结果是一样的,如果数据表没有主键,则 COUNT(1)的执行效率更高
自定义别名“AS 别名”
-- 为了直观的显示输出数字的含义,可自定义别名
select count(*|1|列名) AS allrows from 表名称;
2、SUM( )
select SUM(字段名) from stu;
	-- select sum(age),sum(s_id) from stu;
    -- select sum(age+IFNULL(s_py)) as totle from stu;
IFNULL(字段名,替换数字)
select sum(age+IFNULL(s_py)) as totle from stu;
	-- NULL不能当做数字相加,相加需转化为0
3、AVG( )
select AVG(字段名) from 表名称;
	-- 注意结合 “AS 别名” 和 “IFNULL(字段名,替换数字)”
4、MAX( )
select MAX(age) from stu;
	-- 注意结合 “AS 别名” 和 “IFNULL(字段名,替换数字)”
5、MIN( )
select MIN(age) from stu;
	-- 注意结合 “AS 别名” 和 “IFNULL(字段名,替换数字)”

分组查询

select 字段1,字段2··· from 表名称 GROUP BY 字段1,字段2···;
select age,AVG(hight) GROOUP BY age;
	-- 按年龄进行分组,并计算出每组年龄的平均身高,,,这是和AVG()函数组合使用的情况

HAVING子句

select 字段1,字段2··· from 表名称 GROUP BY 字段1,字段2··· [HAVING 条件表达式];
	-- HAVING子句是可选的
select age,AVG(hight) from stu GROUP BY age HAVING SUM(hight) > 170;
	-- 在输出结果中过滤

LIMIT分页

select 字段1,字段2··· from 表名称 LIMIT [m,]n;
	-- LIMIT后面可以跟两个参数
    -- 第一个参数m 是可选的,代表起始索引,若不指定,则使用默认值0,代表第一条记录
    -- 第二个参数 n 是必选的,代表从第m+1条记录开始取n条记录
select * from stu LIMIT 2,5;
	-- 代表记录3-7,这5条记录    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值