2022-09-10
MySQL中的自連接
何謂自連接?
自連接,即為自己查自己,本表查詢本表,
自連接一般使用于何種地方?
例如:如果在設計一張表中,表中的欄位名包含id(省份/市的郵政編碼),title(省份名/市級名),cid(如果前面title是省份名,那么此處為空;如果前面title是市級名,則此處為對應省份名的郵政編碼)
(1)例如將這張表的欄位設計為如下陳述句:
create table areas(id varchar(30) not null primary key,title varchar(30) not null,pid varchar(10));
(2)之后插入資料,陳述句格式為:
insert into areas values("x","x","x");
(3)表內查詢
例如:查詢“某個省的包含的市級資訊”
select c.id,c.title,c.pid,p.title from areas c inner join areas p on c.pid = p.id where p.title = "xx省";
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/506091.html
標籤:MySQL
