MySQL(查询命令)

以什么开头以什么结尾查询

以什么开头 命令: select * from tableName where 字段 rlike '^ch' limit 5;

以什么结尾 命令: select * from tableName where 字段 rlike 'yi$' limit 5;

 

 

 

以别名查询

命令: select 字段 as 别名 from tableName;

 

 

 

 

最大,最小,平均,总数

函数:实现某一特定的功能

max():最大

select max(字段)from tableName;

min():最小

select min(字段)from tableName;

avg():平均

select avg(字段)from tableName;

 

 

 

总数

select sum(字段)from tableName;

 

 

 

排序

命令:select * from tableName order by 字段;

默认是sec 由低到高

order by :排序

 

 

 

 

倒序

select * from tableName order by 字段 desc;

由大到小

 

 

 

查询大于等于50并倒序

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行

 

 

 

 

 

windows mysql配置文件

 

 

 

查询俩个或两个表以上共同拥有的数据

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;

 

posted @ 2022-05-30 20:17  段舒元  阅读(418)  评论(0编辑  收藏  举报