這篇文章主要介紹了postgreSQL中的內(nèi)連接和外連接實(shí)現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。
測試數(shù)據(jù):
city表:
create table city(id int,name text);
insert into city values(0,'北京'),(1,'西安'),(2,'天津'),(3,'上海'),(4,'哈爾濱'),(5,'西藏')
person表:
create table person(id int,lastname char(20));
insert into person values(0,'Tom'),(2,'Lily'),(3,'Mary'),(5,'Coco');
select * from city;
1select * from person;
一:內(nèi)連接:
1.inner join
inner join(等值連接) 只返回兩個表中聯(lián)結(jié)字段相等的行
sql語句:
1select * from city inner join person on city.id = person.id;
也可以寫成:
1select * from city join person on city.id = person.id;
結(jié)果如下:
從結(jié)果可以看出,表格中顯示出了city.id=person.id的記錄,它顯示出了符合這個條件的記錄。
二:外連接:
1.full outer join
full outer join(全外連接)返回參與連接的兩個數(shù)據(jù)集合中的全部數(shù)據(jù)
sql語句:
1select * from city full outer join person on city.id = person.id;
也可以寫成:
1select * from city full join person on city.id = person.id;
結(jié)果如下:
從結(jié)果可以看出,全外連接得到了city和person表中的全部數(shù)據(jù)
2.left outer join
left outer join(左連接) 返回包括左表中的所有記錄和右表中連接字段相等的記錄
sql語句:
1select * from city left outer join person on city.id = person.id;
也可以寫成:
1select * from city left join person on city.id = person.id;
結(jié)果如下:
從結(jié)果可以看出,左外連接和全外連接的結(jié)果一模一樣?
我們在給person中添加一行數(shù)據(jù):
1insert into person values(9,'Kiki');
在重新執(zhí)行:
1select * from city full join person on city.id = person.id;
結(jié)果如下:
1select * from city left join person on city.id = person.id;
結(jié)果如下:
兩個結(jié)果對照著看,left join顯示出了city中的所有記錄和person連接字段相等的記錄
3.right outer join
right outer join(右連接) 返回包括右表中的所有記錄和左表中連接字段相等的記錄
sql語句:
1select * from city right outer join person on city.id = person.id;
也可以寫成
1select * from city right join person on city.id = person.id;
結(jié)果如下:
從結(jié)果可以看出,person中的記錄被全部顯示出來,而city中的顯示的數(shù)據(jù)是根據(jù)連接字段相等的記錄
補(bǔ)充:PostgreSQL表連接:內(nèi)連接,外連接,自連接,交叉連接
搜了搜,基本上都是寫內(nèi)連接、外連接、交叉連接這三種類型,但我發(fā)現(xiàn)PostgreSQL還有自連接。不妨一并寫來做個記錄。
先說概念:
內(nèi)連接,就是兩個表逐行匹配,匹配上的內(nèi)容都顯示,沒有匹配的都不顯示。
外連接有三種,左外連接,右外連接,全外連接。
左外連接是以左表為基礎(chǔ),左表內(nèi)容全部顯示,右表有匹配到左表的則顯示,否則不顯示。
右外連接是以右表為基礎(chǔ),右表內(nèi)容全部顯示,左表有匹配到右表的則顯示,否則不顯示。
全外連接是以兩表為基礎(chǔ),顯示三部分內(nèi)容,一部分是內(nèi)連接的內(nèi)容,即兩表匹配的內(nèi)容,一部分是左表有而右表無的,一部分是左表無右表有的。
自連接是逐行,用當(dāng)前這行數(shù)據(jù)和這個表中其他行進(jìn)行匹配。
交叉連接最省事,笛卡爾積,左表m行右表n行,則結(jié)果是m*n行。
下面展示具體例子來幫助理解。
下面是兩個表的內(nèi)容。
mydb=# select * from weather;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 43 | 57 | 0 | 1994-11-29
Hayward | 37 | 54 | | 1994-11-29
(3 行記錄)
mydb=# select * from cities;
name | location
---------------+-----------
San Francisco | (-194,53)
London | (0,51)
(2 行記錄)
內(nèi)連接有兩種寫法:
mydb=# SELECT *
mydb-# FROM weather, cities
mydb-# WHERE city = name;
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
(2 行記錄)
mydb=# SELECT *
mydb-# FROM weather INNER JOIN cities ON (weather.city = cities.name);
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
(2 行記錄)
外連接有三種:左外連接,右外連接,全外連接。
mydb=# SELECT *
mydb-# FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
Hayward | 37 | 54 | | 1994-11-29 | |
(3 行記錄)
mydb=# select * from weather right outer join cities on(weather.city=cities.name);
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
| | | | | London | (0,51)
(3 行記錄)
mydb=# select * from weather full outer join cities on(weather.city=cities.name);
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
Hayward | 37 | 54 | | 1994-11-29 | |
| | | | | London | (0,51)
(4 行記錄)
表交叉連接:
mydb=# SELECT *
mydb-# FROM weather, cities;
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | London | (0,51)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | London | (0,51)
Hayward | 37 | 54 | | 1994-11-29 | San Francisco | (-194,53)
Hayward | 37 | 54 | | 1994-11-29 | London | (0,51)
(6 行記錄)
表自連接:
mydb=# SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
mydb-# W2.city, W2.temp_lo AS low, W2.temp_hi AS high
mydb-# FROM weather W1, weather W2
mydb-# WHERE W1.temp_lo < W2.temp_lo
mydb-# AND W1.temp_hi > W2.temp_hi;
city | low | high | city | low | high
---------------+-----+------+---------------+-----+------
San Francisco | 43 | 57 | San Francisco | 46 | 50
Hayward | 37 | 54 | San Francisco | 46 | 50
(2 行記錄)
文章來源:腳本之家
來源地址:https://www.jb51.net/article/204854.htm
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!