-- 1.查看所有的商品
SELECT pid,pname,price,category_id FROM product;
SELECT *from product;
-- 2.查询商品名和商品价格
SELECT pname,price from product;
-- 3.别名查询,使用的关键词是as(as可以省略)
-- 3.1表别名:
select * from product as p;
SELECT * FROM product p;
SELECT p.id,u.id FROM product p,user u;
-- 3.2列别名:
SELECT pname as '商品名',price '商品价格',price from product;
-- 4.去掉重复值-distinct
select distinct price from product;
select distinct * from product;
-- 5.查询结果是表达式(运算查询):
-- 将所有的商品加价10元进行显示
SELECT pname,price+10 new_price from product;
-- 将所有商品的价格上调10%
SELECT pname,price*1.1 as new_price from product;
-- 6.算术运算符
SELECT 6+2;
SELECT 6-2;
SELECT 6*2;
SELECT 6/2;
SELECT 6%4;
-- 7.查询表中信息
-- 查询商品名称为
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;
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;
-- 8.求最小/最大值
-- 使用least求最小值
SELECT least(10,20,15,5);
SELECT least(10,null,20); -- 如果求最小值时,有个值为null,则不会进行比较,结果直接为null
-- 使用greatest求最大值
SELECT greatest(10,20,30,55) as big_number;
SELECT least(10,null,20); -- 如果求最大值时,有个值为null,则不会进行比较,结果直接为null
-- 9.排序查询
-- 使用价格排序(降序)
SELECT * FROM product order by price desc;
-- 在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id desc;
-- 显示商品的价格(去重复),并排序(降序)
SELECT distinct price from product order by price desc;
-- 10.count()的用法
-- 查询商品的总条数
SELECT count(pid) from product;
SELECT count(*) from product;
-- 查询结果大于200商品的总条数
select count(pid) from product where price >200;
-- 11.sum()的用法
-- 查询分类为'c001'的所有商品的总和
select * from product where category_id='c001';
select sum(price) from product where category_id='c001';
-- 12.最大值、最小值。平均值
-- 查询商品的最大价格
SELECT max(price) from product;
-- 查询商品的最小价格
SELECT max(price) max_price,min(price) min_price from product;
-- 查询分类为‘c002’所有商品的平均价格
SELECT avg(price) from product where category_id='c002';
-- 13.分组查询
-- 统计各个分类商品的个数
-- 注意 分组之后 select的后边只能写分组字段和聚合函数
SELECT category_id,count(pid) from product GROUP BY category_id;
-- 14.统计各个分类商品的个数,且只显示个数大于4的信息
-- SQL的执行顺序:from->GROUP BY->count(pid)->SELECT->having->ORDER BY
SELECT
category_id,count(pid) cnt
from
product
GROUP BY
category_id
having
count(pid)>4
GROUP BY
cnt;
-- 分页查询-LIMIT
-- 查询product表的前5条信息
select * FROM product limit 5;
-- 从第4条开始显示,显示5条
SELECT * FROM product limit 3,5;
-- 分页显示
SELECT * from product limit 0,60;-- 第一页--->(1-1)*60
SELECT * from product limit 60,60;-- 第二页--->(2-1)*60
SELECT * from product limit 120,60;-- 第三页--->(3-1)*60
MYSQL中DQL的操作
最新推荐文章于 2024-07-09 11:09:43 发布