合并结果集

  1. 合并结果集
  • 合并结果集就是把两个select 语句的查询结果合并到一起
  1. 合并方式

    • union : 合并时去除重复记录sql select * from table1 union select * from table2;

    • union all : 合并时不去除重复记录sql select * from table1 union all select * from table2;

多表联查

  1. 多表联查

    • 同时将两个表的数据都查出来: 这种情况就属于笛卡尔集查询, 不限定查询条件sql select * from table1,table2;
  2. 笛卡尔集查询

    • 假设集合A={a,b} 集合B={0,1,2}
    • 则两个集合的笛卡尔积为{(a,0),(a,1),(a,2),(b,0)(b,1)(b,2)}
    • 可以扩展到多个集合的情况```javascript // 如何解决上面的问题,需要加入查询的where条件 select * from table1,table2 where table1.id = table2.id;

// 别名写法 select * from table1 t1,table2 t2 where t1.id = t2.id;

  1. <a name="d08fe29e"></a>
  2. ### 连接查询
  3. 1. 内连接
  4. - 求两个表的交集,两个表共有的数据
  5. - 语句 inner join 查询条件使用on 来定义```sql
  6. select * from table1 t1 inner join table2 t2 on t1.key = t2.key
  1. 多表连接(内连接-多表)

    • 多个表之间的关系查询,查出公用的数据 99连接法sql select * from student st,score sc,category ca where st.id = sc.sid and sc.cid = ca.id;

    • 内连接写法sql select * from student st inner join score sc on st.id = sc.sid inner join category ca on sc.cid = ca.id;

  2. 左连接

    • A与B A 表查出的,B表查出符合条件的 然后柔和在一起 么有的数据补空sql select * from table1 t1 left join table2 t2 on t1.key = t2.key

left_join.jpg

  1. 右连接
    • A与B A 表查出公共部分的,B表查出所有符合条件的sql select * from table1 t1 right join table2 t2 on t1.key = t2.key
      right_join.jpg