数据库(连接)
連接:
? 為什么需要連接?:因為我們需要的數據在不同的表中
內連接:
? select 列A from 表A inner join 表B on 條件1=條件2
?? 在內連接中可以省去 inner?
?? 注:on 必須和join 一起出現? 他沒有單獨存在的意義
外連接:
? left join :左連接?
? right join: 右連接
? full join :全外連接? 注:在mysql中 并不支持
自連接:
? select 列名 from 表A jion 表A on 條件
?
/*內連接:
基本語法:select 列A from 表A inner(inner可以省略) join 表B? on 條件=條件
注:這里的 on 必須和join 一起出現 他沒有單獨存在的意義
*/
/*查找每一個員工的領導是誰?*/
/*使用join on 內連接查詢數據*/
/*select e.eName,e2.eName from emp e JOIN emp e2 on e.mgr=e2.empno*/
/*使用子查詢同樣可以做到*/
?select e2.eName as 員工,(select e.eName from emp e where e.empno=e2.mgr) as 領導 from emp e2
?
/*查詢每個員工屬于哪個部門 并且顯示部門的詳細信息
員工的信息和部門信息不在一個表中 所以會涉及到兩表聯查*/
/*首先使用join 查詢*/
/*select * from emp e JOIN dept d on e.deptNo=d.deptNo*/
/*然后使用第二種方法 where 查詢 不使用 join */
select * from emp e ,dept d where e.deptNo=d.deptNo
/*使用 where 方法查詢使用的時間會更少 */
?
/*外連接:
?? left join 左連接 左表有數據而右表沒有數據 他還是會顯示左表的數據
但是右表數據的位置為null
?? right join 右連接 與左連接相反
?? full join 全外連接 在mysql種沒有此方法
*/
/*使用左連接來查詢
select * from emp e left join dept d on e.deptNo=d.deptNo
在右表 dept 位置沒有數據的時候顯示為 null 但是左表依然會顯示*/
/*select * from emp e join dept d on? e.deptNo=d.deptNo
然而 join方法 只要一方為 null 左右數據都不會顯示
*/
/*使用右連接來查詢
select * from emp e right join dept d on e.deptNo=d.deptNo
在左表 emp 位置沒有數據的時候 左表全部數據都為 null 右表數據還是會顯示
*/
在數據庫中會涉及到多表聯查 有兩種方法可以使用 ①:join ②:where
使用 inner join 方法:
??? select * from 表A join 表B on 條件A=條件B join 表C on 條件C=條件D
?? 注:查表C 的時候不能寫在 表B后面 而是應該 跟在 on 條件以后
使用 left join 方法:
?? select * from 表A left join 表B on 條件A=條件B left join 表C on 條件C=條件D
?? 注:右查詢和inner join 差不多 只是 inner join是內查詢 而 left join 是外查詢
使用? where 方法:
?? select * from 表A,表B where 條件A and 條件B
轉載于:https://www.cnblogs.com/chenyangpeng/p/5483294.html
總結
- 上一篇: Android JSON原生解析的几种思
- 下一篇: 数据结构笔记--栈的总结及java数组实