mysql——单表查询操作


1.简单查询
	所有字段的查询
		use dsx_test;
		select * from `8月成交数据`;

	指定字段的查询
		select 日期,业务组,成交额 
		from 8月成交数据;

	去除重复记录的查询
		select distinct 业务组 
		from 8月成交数据;

	使用算数运算符的查询(+,-,*,/,%)
		select 业务组,成交额*2 
		from 8月成交数据;

	使用字段别名的查询(中括号内的可省略)
		select 业务组,成交额*2 [as] 双倍成交额 
		from 8月成交数据;

	设置数据显示格式的查询(用concat函数)
		select concat('业务组:',业务组,',成交额:',成交额) 
		from 8月成交数据;


2.对查询结果排序(order by)
	按照指定的单字段排序
		select 日期,业务组,成交额 
		from 8月成交数据 
		order by 成交额 asc;/* asc是上升,desc是下降,默认是asc */

	按照指定的多字段排序
		select 日期,业务组,成交额,应收利息 
		from 8月成交数据 
		order by 成交额 asc,应收利息 desc;/* 成交额由小到大排序,成交额相等的时候,按照应收利息从大到小排序 */


3.条件查询(where)
	使用比较运算符的查询
		select * from 8月成交数据 
		where binary 业务组='西部战区-重庆-重庆一组';/* mysql默认不区分字母大小写,这里binary是强制mysql区分大小写 */

	使用[not]between...and...的范围查询
		select * from 8月成交数据
		where 应收利息 between 1000 and 5000;

	使用[not]in的指定集合查询
		select * from 8月成交数据
		where 业务组 in('西部战区-重庆-重庆一组','东部战区-南京-南京二组');
		select * from 8月成交数据
		where 业务组 not in('西部战区-重庆-重庆一组','东部战区-南京-南京二组');

	使用 is[not]null的空值查询
		select * from 8月成交数据
		where 逾期金额 is null;
		select * from 8月成交数据
		where 逾期金额 is not null;

	使用[not]like的模糊查询
		使用%通配符的模糊查询
			select * from 8月成交数据
			where 日期 like '2020年%';/* 查找日期中以‘2020年’开头的记录 */
			select * from 8月成交数据
			where 日期 like '%2日';/* 查找日期中以‘2日’结尾的记录 */
			select * from 8月成交数据
			where 日期 like '%2日%';/* 查找日期中包含‘2日’的记录 */
		使用_通配符的模糊查询('_'只能匹配一个字符)
			select * from 8月成交数据
			where 小组 like '__三%';/* 查找第三位是‘三’并总字符数大于等于3位的记录 */
		使用not like的模糊查询
			结合'%''_'使用

	使用 and 的多条件查询
		select * from 8月成交数据
		where 逾期金额>0 and 应收利息>5000;

	使用 or 的多条件查询
		select * from 8月成交数据
		where 逾期金额>0 or 应收利息>5000;


4.限制查询(limit)
(指定查询结果中显示记录的初始位置和记录显示的行数)
	不指定初始位置的限制查询
		select * from 8月成交数据
		where 逾期金额 > 1000 limit 5;/* 限制显示5行 */

	指定初始位置的限制查询(start_index)
		select * from 8月成交数据
		where 逾期金额 > 1000 limit 2,9;/* 限制显示9行,并显示第3行-第11行的9条记录 */	


5.函数查询
	使用单行函数查询
		字符串函数(这里仅介绍使用concat(),length(),lower(),upper(),replace(),substring())
			select concat('业务组:',业务组,',成交额:',成交额) 
			from 8月成交数据;/* 将业务组、成交额拼成一个新的字符串 */
			select * from 8月成交数据 
			where length(应收利息)=4;/* 字符串的长度 */
			select lower(业务组),upper(战区) 
			from 8月成交数据
			where 逾期金额>1000;/* 大小写 */
			select 城市,replace(城市,'合肥','庐州') as 新城市
			from 8月成交数据
			where 城市='合肥';/* 将城市为合肥的改为庐州 */
			substring(s,start,length)指从字符串s的第start个位置截取length个长度的子字符串
			select 小组,substring(小组,1,2) as 新小组
			from 8月成交数据
			where 小组 like '杭州%';
		数值函数
			使用abs(),ceil(),floor(),mod(),pi(),pow()函数的使用(绝对值,向上取整,向下取整,取余数,圆周率,乘方)
				select abs(-2),ceil(3.3),floor(3.3),mod(5,2),pi(),pow(2,3) [from dual];
			rand()函数的使用
				select rand(),rand(),rand();/* 返回0~1之间的随机数,括号内填随机种子 */
			round(num,n)函数的使用(对数字num进行四舍五入,并保留n位小数)
				select round(2.22222,2) [from dual];
			truncate(num,n)函数的使用(对数字num进行截断,截断到小数点后n位)
				select truncate(2.23456,0),truncate(2.23456,3);
		日期与时间函数
			curdate(),curtime(),now()函数的使用(返回当前日期、时间、日期和时间)
				select curdate(),curtime(),now();
			sysdate()函数的使用(返回该函数执行时的日期和时间)
				select now(),sysdate(),sleep(2),sysdate(),now();
			dayofyear(),week()函数的使用(一年的第几天,一年的第几周)
				select dayofyear('2023-03-15'),week('2023-03-15');
			date_add(),date_sub()函数的使用(实现日期的加减运算)
				/* 细节太多了,先不学 */

			datediff(date1,date2)函数的使用 (返回date1与date2之间的间隔天数,有正负值)
				select datediff('2023-03-10',now());
		流程函数
			if(condition,t,f)函数的使用 (如果条件condition为真,则返回t,否则返回f)
				select ename,deptno,sal,if(sal>2000,'high','low')sal_level from emp where deptno=20;
			ifnull(value1,value2)函数的使用 (如果value1不为Null,则返回value1,否则返回value2)
				select ename,deptno,job,sal*12+ifnull(comm,0) as year_income
				from emp
				where deptno=30;
			nullif(value1,value2)函数的使用 (如果value1等于value2,返回Null,否则返回value1)
				select nullif(1,1),nullif(1,2) from dual;
			case...when...then...()函数的使用
				case when condition1 then result1
					when condition2 then result2
					else result end /* 如果条件condition1为真,则返回result1,如果条件condition2为真,则返回result2,否则返回result */
				select ename,deptno,sal,case sal>=2000 when true then 'high' else 'low' end sal_level 
				from emp 
				where deptno=20;

				case value when value1 then result1
					when value2 then result2
					else result end /* 如果value等于value1,则返回result1,如果value等于value2,则返回result2,否则返回result */
				select ename,deptno,sal,case when sal>=2000  then 'high' else 'low' end sal_level 
				from emp 
				where deptno=20;

		其他函数
			/* 需要的时候百度 */

	使用多行函数查询 (亦名为分组函数,对一组数据进行运算,只返回一个结果)
		count()函数的使用
			count(*)返回表中记录的总数目
			count(exp)返回表达式exp值非空的记录数目
			count(distinct(exp))返回表达式exp值不重复、非空的记录数目
			select count(*),count(deptno),count(distinct(deptno)) from emp;
		sum()与avg()函数的使用
			sum(exp)返回表达式exp值的总和
			sum(distinct(exp))返回不重复的表达式exp值的总和
			avg(exp)返回表达式exp值的平均数
			avg(distinct(exp))返回不重复的表达式exp值的平均数
			select sum(comm),sum(distinct comm),avg(comm),avg(distinct comm) from emp;
		max()与min()函数的使用
			select max(sal),min(sal) from emp;


6.分组查询
insert into emp values(7369,'Smith','clerk',7902,'1980-12-17',800,null,20),
                      (7499,'Allen','salesman',7698,'1981-02-20',1600,300,20),
                      (7521,'Ward','salesman',7698,'1981-02-22',1250,500,30),
                      (7566,'Jones','manager',7839,'1981-04-02',2975,null,20);
	使用group by的简单分组查询
		select deptno 
		from emp 
		group by deptno;
		select deptno,job 
		from emp 
		group by 1,2;

	使用group by与统计函数的分组查询
		使用group by与统计函数的分组查询
			select deptno,count(*),sum(sal),avg(sal),max(sal),min(sal)
			from emp
			group by deptno;
		使用group by与group_concat()的分组查询
			select deptno,count(*),group_concat(ename) enams
			from emp
			group by deptno;/* group_concat(),可以将每个部门的员工姓名成功地显示出来。 */

	使用group byhaving by与统计函数的分组查询
		select deptno,count(*),sum(sal),avg(sal),max(sal),min(sal)
		from emp
		group by deptno
		having avg(sal)>1300;/* 对group by的内容进行过滤  */


注意:
1.where过滤行,having过滤分组,having支持所有where的操作;
2.having子句出现在group by子句之后,而where子句要出现在group by子句之前。
3.MYSQL中各子句的执行过程由先到后依次为:
	from-->where-->group by-->having-->select-->order by



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值