> 文档中心 > MySQL多表查询

MySQL多表查询

多表查询
内连接:select * from 表1,表2,表3 where 表1列1=表2列2 and 表2列2=表3列3;

Select * from 表1 inner join 表2 on 表1列1=表2列2;

mysql> select * from stus inner join ary on stus.id=ary.ass_id ;

外左连接:select * from 表1 left join 表2 on 表1列1=表2列2;

mysql> select * from stus leftjoin ary on stus.id=ary.ass_id ;

外右连接:select * from 表1 right join 表2 on 表1列1=表2列2;

mysql> select * from stus rightjoin ary on stus.id=ary.ass_id ;

子查询:select * from 表 where 字段 [not] in (…);

mysql> select * from stus where id in (select id from stus where name="李丽") ;

语句结构

Select * from 表 where 条件语句 group by 分组列 having 聚合函数-条件语句 order by 排序列 limit 取前n数据;

Select * from 表 where 条件语句1 or[and] 条件语句2 group by 分组列 having 聚合函数-条件语句 order by 排序列 limit 取前n数据;

先where条件语句 过滤 数据,再针对过滤后的数据,分组。。再对分组后 数据,再having过滤,再对结果数据,再排序;

(条件:分组前用where;分组后不用where)

字符串 find_in_set():

精确模糊查询:select * from 表 where find_in_set(‘值’,’字段’); (备注:没有满足条件,返回值为空;)

mysql> select * from stus where find_in_set(1624,id) ;+------+----------+| id   | name     | +------+----------+| 1624 | yangming |+------+----------+4 rows in set (0.01 sec)

读完本文有收获吗?请转发分享更多人

软件测试分享圈
在这里插入图片描述

读书笔记网