以什么开头 命令: select * from tableName where 字段 rlike '^ch' limit 5;
以什么结尾 命令: select * from tableName where 字段 rlike 'yi$' limit 5;
命令: select 字段 as 别名 from tableName;
函数:实现某一特定的功能
select max(字段)from tableName;
select min(字段)from tableName;
select avg(字段)from tableName;
select sum(字段)from tableName;
命令:select * from tableName order by 字段;
默认是sec 由低到高
order by :排序
select * from tableName order by 字段 desc;
由大到小
select * from student where score>=50 order by score desc;
select distinct 字段 from tableName;
distinc 去重
聚合函数:group by
having与where的区别
WHERE 与 HAVING 的根本区别在于:
WHERE 子句在 GROUP BY 分组和聚合函数 之前 对数据行进行过滤;
HAVING 子句对 GROUP BY 分组和聚合函数 之后 的数据行进行过滤。
having 是根据聚合函数 group by结合使用的
实战:以年龄查询学生的平均分大于50并且以平均分倒序
select age,avg(score) as 平均分 from student group by age having 平均分>50 order by 平均分 desc;
查询员工里有多少名男女
select gender as 性别,count(gender) as 人数 from employees group by gender;
查询 employees表每个last_name一样的有多少人显示10行
inner join,⼜叫内连接的部分,主要是获取两个表中字段匹配关系的表。查询关联字段共同拥有的数据
select shop_name,good_name,good_type,price from shop inner join goods on shop.id=goods.shop_id;
查询店铺名称,快递名称,快递电话
select shop_name,name,phone from shop inner join goods on shop.id=goods.shop_id inner join logistic on goods.id=logistic.good_id;
查询shop,goods表的所有字段 2、以商品的价格作为倒序排序
select * from shop inner join goods on shop.id= goods.shop_id order by goods.price desc;