SQL - Self Join
Self-Join
用的時機:
- query hierarchical data (query 一層一層的data)
- 自己 table 內的 rows 比較
- 條件
- 在做 self-join, 一定要用 table alias (代號), 要不然會出現 error, 因為都是自己 query 自己
select concat(p1.first_name, ' ', p1.last_name) as 'p1 table',
concat(p2.first_name, ' ', p2.last_name) as 'p2 table'
from person p1
join person p2
on p1.first_name = p2.last_name
order by p1.first_name
;
留言
張貼留言