– 计算列的值
select 列名1(±*/)列名2 from 表名;
– 起别名
select 列名 as 列别名 from 表名 as 表别名;
注:as可省略
– 查询库存大于20的商品信息
select * from 表名 where 库存>20;
– 查询品牌为华为
select *from 表名 where 品牌=华为;
– 查询价格在4000-6000的商品
select * from 表名 where price>=4000 and price<=6000;
select * from 表名 where 价格 between 4000 and 6000;
– 查询库存为14 30 23的商品
select from 表名 where 库存=14 or 库存=30 or 库存=23;
select * from 表名 where 库存 in(14,30,23);
– 查询名称以小米开头
select * from 表名 where 商品名称 like ‘小米%’;
– 聚合函数查询语法
select 函数(列名) from 表名 where 条件;
例:select avg(age) from student;
条件可以省略,视情况而定;
– 计算表中数据条数
select count() from 表名;
– 获取最高价格的商品
select * from 表名 order by 价格 desc limit 1;
selecr max(价格) from 表名;
– 获取最低价格的商品
select * from 表名 order by 价格 limit 1;
select min(价格列名) from 表名;
mysql默认是asc排序
– 获取总库存
select sum(库存数量列名) from 表名;
– 获取品牌为小米的平均价格
select avg(价格) from 表名 where 品牌=小明;
– 按照库存排序
select * from 表名 order by 库存 desc;
– 查询名称包含手机 再按价格排序
select * from 表名 where 名称 like '%手机%'order by 价格 desc;
– 按价格升序 如果价格相同 按照库存降序
select *from 表名 order by 价格 asc,库存 desc;
– 分组查询语法
– select 列名 from 表名 where 条件 group by 分组别名 having 分组后条件 order by 排序列名 排序方式;
分组列名必需出现在查询中,select中处理分组名只能有聚合函数
– 分页查询语法
select *from 表名 limit 0,3;
limit 自那条记录开始 可以理解为第一条为0,如果是11-20
select *from 表名 limit 10,10;
第二位数为查询几个
– 主键约束
– create table 表名 (
– 列名 数据类型 primary key
– );
– 删除主键约束
dlter table 表名 drop primary key;
mysql基础2
于 2023-05-10 23:16:05 首次发布