MySQL 查询语句

查询
Select *from 表名
Select * from 表名  where 列名
Select * from 表名where 列名 like …
Select 列名 from 表名

-- 查询部分字段: select 字段名1,字段名2 from goods;

select goodsName,price from goods;

-- 起字段别名: select 字段名 as 别名 from goods;

select goodsName as'商品名称',price as'价格’ from goods;

- 注意: 别名的引号可以省略

select goodsName as 商品名称,price as 价格 from goods;

注意:as关键字也可以省略;

select goodsName 商品名称,price 价格 from goods;

-- 去重:select distinct(字段名) from goods;

--效果:将目标字段内重复出现的数据值保留一份显示

-- 需求:显示所有公司名字

select distinct(company) from goods;

带条件and 与或者 查询

select * from goods where price = 30 and company = '并夕夕'

-- 范围查询

select *from goods where price between 30 and 100

-- between....and..... 的范围必须是从小到大

select * from goods where price between 100 and 30

排序查询

-- select * from 表名 order by 列1 asc|desc,列2 asc|desc,.......

-- 说明:order by 排序,asc:升序,desc:降序

-- 注意:排序过程中,支持连续设置多条排序规则,但离order by 关键字越近,排序数据范围越大

select * from goods order by price desc;
select * from goods order by price desc,  count asc

-- 注意,默认排序为升序,asc可以省略

select * from goods order by price desc,count;

- 注意: 统计数据总数,建议使用*,如果使用某一特定字段,可能会造成数据总数错误!

select count(*) from goods;
select count(remark) from goods;

-- 最高商品价格: max(字段): 查询最大值

select max(price) from goods;

-- 最低商品价格: min(字段): 查询最小值

select min(price) from goods;

-- 商品平均价格: avg(字段): 求平均值

select avg(price) from goods;

-- 一次性口罩的总数量: sum(): 求和

-- 注意: 此处的 count 是数据表中字段名!

select sum(count) from goods where remark like %一次性%';

-- 说明:group by :分组

-- 一般情况,是哦那个字段分组,那么只有该字段可以在*的位置使用,其他字段没有实际意义

-- 分组操作多和聚合函数配合使用

select count(*) from goods group by company;
select * from goods;
select company, count(*) from goods gtoup by company;

-- 分页

--过度需求:获取前五条数据

select * from goods limit 0,5;

-- 注意:如果默认从第一条数据开始获取,则0可以省略

select * from goods limit 5;

-- 需求:

select * from goods limit 4,6

-- 扩展1: 根据公式limit显示某页的数据

--已知:每页显示m条数据,求:显示第n页的数据

-- select * from 表名 limit (n-1)* m,n

-- 实列,每页显示 4条数据,求展示第二页的数据内容

select * from goods limit 0.4; 
select * from goods limit 4,4;
select * from goods limit 8.4;
selcet * from goods limit 12,4;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值