连表查询

1.左右边链接,语句

  select * from 主表 left outer join 从表 on 主表.XX = 从表.XX

  可简写为: select * from 主表 left join 从表 on 主表.XX = 从表.XX

 示例1:

select * from info left join depart on info.depart_id = depart.id

示例2:根据自己需要的列进行显示

select info.name,info.email,info.age,depart.title from info left join depart on info.depart_id = depart.id;

2.内链接,语句

   select * from 表 innter join 表 on 条件

select * from info inner join depart on info.depart_id=depart.id;

+----+-----------+------------------+------+-----------+----+--------+
| id | name      | email            | age  | depart_id | id | title  |
+----+-----------+------------------+------+-----------+----+--------+
|  1 | 武沛齐    | wupeiqi@live.com |   19 |         1 |  1 | 开发   |
|  2 | 于超      | pyyu@live.com    |   49 |         1 |  1 | 开发   |
|  3 | alex      | alex@live.com    |    9 |         2 |  2 | 运营   |
|  4 | tony      | tony@live.com    |   29 |         1 |  1 | 开发   |
|  5 | kelly     | kelly@live.com   |   99 |         3 |  3 | 销售   |
|  6 | james     | james@live.com   |   49 |         1 |  1 | 开发   |
|  7 | 李杰      | lijie@live.com   |   49 |         1 |  1 | 开发   |
+----+-----------+------------------+------+-----------+----+--------+

3.目前为止sql执行顺序

  join ,on,where , group by , having , order by , limit

4.联合查询:

  select name from info union select title from depart;    --显示的列数必须相同

 select id from depart union select id from info;

  --自动去重

select id from depart union all select id from info;  --不进行去重

posted @ 2021-11-19 15:40  A熙  阅读(117)  评论(0)    收藏  举报