项目四总结:MYSQL数据表的检索

目录

一、查看表中所有字段数据

二、查看表中部分字段数据

三、连接条件:and(并且)和or(或者)与关系运算符

四、在 值1,...,值n 里 和 在 值1 和 值2 之间

五、为空和不为空

六、模糊查询

八、正序排序和倒序排列

七、限制条数(2种)

八、去重

九、聚合函数

十、分组

十一、为字段名和表取别名

十二、交叉连接

十三、内连接

十四、左连接

十五、右连接

十六、复合条件

十七、子查询   in 

十八、子查询   exists

十九、子查询  any

二十、子查询  all


一、查看表中所有字段数据

select * from 表名 where 条件;

表名后可加where条件



select * from 表名 where 条件 order by 字段名 limit 条数;

二、查看表中部分字段数据

select 字段名1,字段名2,...,字段名n from 表名 where 条件;

表名后可加where条件



select 字段名1,字段名2,...,字段名n from 表名 where 条件 order by 字段名 limit 条数;

三、连接条件:and(并且)or(或者)与关系运算符

1.连接条件: and (或者) 

条件 == 替换为==> 条件1 and 条件2

select * from 表名 where 条件1 and 条件2;

2.连接条件: or (或者) 

条件 == 替换为==> 条件1 or 条件2

select * from 表名 where 条件1 or 条件2;

条件

关系运算符

   等于               字段名 = 值

   不等于           字段名 != 值

   小于               字段名 < 值

   小于等于        字段名 <= 值

   大于               字段名 > 值

   大于等于        字段名 >= 值

四、在 值1,...,值n 里  在 值1 和 值2 之间

1.在 值1,...,值n 里

字段名 in (值1,...,值n);

select * from 表名 where 字段名 in (值1,...,值n);

2.在 值1 和 值2 之间

字段名 between 值1 and 值2;

select * from 表名 where 字段名 between 值1 and 值2;

 

五、为空不为空

1.为空

字段名 is null

select * from 表名 where 字段名 is nul;

2.不为空

字段名 is not null

select * from 表名 where 字段名 is not nul;

 

六、模糊查询

1.%代替任意长度字符串


字段名 like “...%...”

select * from 表名 where 字段名 like “...%...”;

2._代替一个字符

select * from 表名 where 字段名 like “..._...”;

字段名 like “..._...”

 

八、正序排序倒序排列

1.正序排列

order by 字段名

select * from teacher order by 字段名;

order by 字段名 asc

select * from teacher order by 字段名 asc;

2.倒序排列

order by 字段名 desc

select * from teacher order by 字段名 desc

 

七、限制条数(2种)

1.limit 条数

select * from teacher limit 条数

2.limit 偏移量,条数

select * from teacher limit 偏移量,条数

八、去重

select distinct 字段名 from 表名;

九、聚合函数

1.数量:count(字段名)   count(* or 0)

select count(字段名) from 表名;

2.求和:sum(字段名)

select sum(字段名) from 表名;

3.平均值:avg(字段名)

select avg(字段名) from 表名;

4.最小值:min(字段名)

select min(字段名) from 表名;

5.最大值:max(字段名)

select max(字段名) from 表名;




select max(字段名),min(字段名)... from 表名;

 

 

 

 

 

十、分组

group by 字段名 having 条件;

 

十一、为字段名表取别名

1.为字段取别名(as可以省略)

字段名(可写函数)as 字段别名

2.为表取别名(as可以省略)

表名 as 表别名

 

十二、交叉连接

select * from 表名1 cross join 表名2;

 

十三、内连接

select * from 表名1 inner join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;

 

十四、左连接

select * from 表名1 left join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;

十五、右连接

select * from 表名1 right join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;

 

十六、复合条件

select --> from --> join --> on --> where --> group by --> having --> order by --> limit

十七、子查询   in 

select * from 表名 where 字段 in (查询语句);

十八、子查询   exists

select * from 表名 where exists (查询语句);

 

十九、子查询  any

select * from 表名 where 字段 > any (查询语句);

二十、子查询  all

select * from 表名 where 字段 > all (查询语句);

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值