Sql基本查询----DQL(Data Query Language)

在这里插入图片描述

基本查询DQL

1:简单查询
	查全表
		select * from product;
	指定字段查询
		select pid,pname,price from product;
	别名查询
		列别名
			select pname,price+10 as new_price from student;(as可有可无)
		表别名
			select pid ,pname from prodeuct as p;
	去重查询
		关键字
			distinct
		操作
			去除重复行
				select distinct * from product;
			去除重复的列
				select distinct price from product;
2: 运算符操作
	算术运算符
		符号
			+  -  *  /  %
		操作
			select 1+2;
			select price * 1.1 from product;
	比较运算符
		符号:
			> >=  <  <=  in  not in   like   is null   is not null   
		操作
			
			-- 查询商品名称为“海尔洗衣机”的商品所有信息:
				select * from product where pname = '海尔洗衣机';
			 
			-- 查询价格为800商品
				select * from product where price = 800;
			
			-- 查询价格不是800的所有商品
				select * from product where price != 800;
				select * from product where price <> 800;
				select * from product where not(price = 800);
				
			-- 查询商品价格大于60元的所有商品信息
				select * from product where price >60;
			
			-- 查询商品价格在200到1000之间所有商品
				select * from product where price between 200 and 1000;  -- 包含200 1000	
				select * from product where price >=200 and price <= 1000;
				select * from product where price >=200 && price <= 1000;
			
			-- 查询商品价格是200或800的所有商品
				select * from product where price in(200,800);
				select * from product where price = 200 or price = 800;
				select * from product where price = 200 || price = 800;
			  
			-- 查询含有‘裤'字的所有商品     % 任意匹配
				select * from product where pname like '%裤%';
			
			--  查询以'海'开头的所有商品
				select * from product where pname like '海%';
			
			-- 查询第二个字为'蔻'的所有商品
				select * from product where pname like '_蔻%';
				
			-- 查询category_id为null的商品
				select * from product where category_id is null ;
				
			-- 查询category_id不为null分类的商品
				select * from product where category_id is not null ;

	逻辑运算符
		符号
			and(&&) or(||) not
		操作
			-- 查询商品价格是200或800的所有商品
				select * from product where price in(200,800);
				select * from product where price = 200 or price = 800;
				select * from product where price = 200 || price = 800;
	位运算符(了解)
		符号
			& |  ^  <<  >>  ~
		操作
			-- 位运算符  二进制
			select 3&5; -- 位与
			select 3|5; -- 位或
			select 3^5; -- 位异或
			select 3>>1; -- 位左移
			select 3<<1; -- 位右移
			select ~3;   -- 位取反
3:排序查询
	关键字
		order by asc| desc
		asc:升序(默认)
		desc(降序)
	特点
		1:如果order by 后跟一个字段,则只会按照该字段的值进行排序,该字段可以是数值类型,字符串类型,日期类型
		2:如果order by 后跟多个字段:order by c1,c2  那就先按照c1来排序,如果c1相同,那就按照c2来排序
	操作
		-- 1.使用价格排序(降序)
				select * from product order by price desc;
		
		-- 2.在价格排序(降序)的基础上,以分类排序(降序)
				select * from product order by price desc, category_id desc;
		
		-- 3.显示商品的价格(去重复),并排序(降序)
				select distinct price from product order by price desc; 
4:聚合查询
	关键字:(如果有null值,不会计算)
		count()
		count(*) count(id) 

		sum()
		sum(price)
		max() min()
		avg()
	操作
		1. 一般情况下,聚合函数和分组函数会结合在一起使用
		2. 单独使用
			-- 1 查询商品的总条数
			select count(pid) count from product;
			select count(*) count from product;
			
			-- 2 查询价格大于200商品的总条数
			select count(pid) from product where price>200;
			
			-- 3 查询分类为'c001'的所有商品的总和
			select sum(price)from product where category_id = 'c001';
			
			-- 4 查询商品的最大价格
			select max(price) from product;
			
			-- 5 查询商品的最小价格
			select min(price) from product;
			
			-- 6 查询分类为'c002'所有商品的平均价格
			select avg(price) from product where category_id = 'c002';
		3. 和分组一起使用
			select category_id,count(*) from product group by category_id order by category_id;

	统计各个分类商品的个数,且只显示个数大于4的信息
			select 
				category_id,count(*)
			from 
				product 
			group by 
				category_id 
			having 
				count(*)>4;
5:分组查询
	关键字
		group by
	特点
		1:分组可以理解为将一张表临时拆分成多张表,拆分的依据就是分组字段。
		2:分组可以根据一个字段,也可以根据多个字段,如果是一个字段,则该字段相同就会分到同一组。如果是多个字段,则多个字段都相同才能分到同一组
			group by province,city
		3:分组之后,select的后边只可以分组字段和聚合操作
	操作
		1:注意,一般情况下,聚合函数和分组函数会结合在一起使用
		select category_id,count(*) from product group by category_id order by category_id;
	分组之后的条件筛选:  分组之后,对分组后的结构进行条件判断不能使用where,必须使用having
		select 
		category_id,count(*)
		from 
			product 
		group by 
			category_id 
		having 
			count(*)>4;
6:分页显示
	关键字
		limit
	操作
		
	-- 分页查询
	-- 查询第五条
	select * from product limit 5;
	-- 从第1条开始显示,显示五条
	select * from product limit 0,5;
7书写顺序
	书写顺序
		select 
			category_id,count(pid) cnt
		from 
			product 
		where
			price>1000
		group by
			category_id
		having
			cnt>5
		order by 
			cnt
		limit 1
		;
			
	执行顺序
		from --> where-> group by-> count()-->having-->select__--->order by -->limti
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值