mysql聯表速查

2021-09-24 23:06:41 字數 1256 閱讀 3976

select a.,b. from a inner join(join) b on a.id = b.id

select a.*,b.* from a left join b on a.id = b.id

select a.*,b.* from a left join b on a.id = b.id where b.id is null

select a.*,b.* from a right join b on a.id = b.id

select a.*,b.* from a right join b on a.id = b.id where a.id is null

select a.id aid,a.age,b.id bid,b.name from  a

left join b

on a.id = b.id

union

select a.id aid,a.age,b.id bid,b.name from a

right join b

on a.id = b.id

select a.id aid,a.age,b.id bid,b.name from  a

left join b

on a.id = b.id

where b.id is null

union

select a.id aid,a.age,b.id bid,b.name from a

right join b

on a.id = b.id

where a.id is null

實際應用中還有這樣一種情形,想得到a,b記錄的排列組合,即笛卡兒積,這個就不好用集合和元素來表示了。需要用到cross join。

笛卡爾積:笛卡爾乘積是指在數學中,兩個集合x和y的笛卡尓積(cartesian product),又稱直積,表示為x × y,第乙個物件是x的成員而第二個物件是y的所有可能有序對的其中乙個成員。

select a.id aid,a.age,b.id bid,b.name from tablea a

cross join tableb b

MySQL聯表查詢

顯示所有員工名字 emp.ename 員工工資 emp.sal 及所在部門的名字 dept.dname 笛卡爾積 emp num dept num 聯表查詢時一定要帶上關聯條件 select ename,sal,dname from emp,dept where emp.deptno dept.de...

MySQL聯表查詢及聯表刪除的方法

mysql聯表查詢及聯表刪除都是經常需要用到的操作,下面對mysql聯表查詢和聯表刪除都作了詳細的介紹分析,希望對您有所幫助。mysql聯表查詢 reference mysql manul 3.2.7.select語法13.2.7.1.join語法 13.2.7.2.union語法 eg1 mysq...

mysql的聯表刪除

聯表刪除 1 從資料表t1 中把那些id值在資料表t2 裡有匹配的記錄全刪除掉 delete t1 from t1,t2 where t1.id t2.id 或delete from t1 using t1,t2 where t1.id t2.id 2 從資料表t1裡在資料表t2裡沒有匹配的記錄查詢...