在項目開發中遇到這麼一個例子,首先產品表html
tb_producthtm
-----------------------------開發
id name產品
1 手機it
2 電腦class
3 筆記本select
第二張表im
tb_product_chain(產品鏈)數據
------------------------------------------------------項目
int product_id parent_product_id
1 1 2
2 1 3
須要新建一個查詢,即把表2中的product_id和parent_product_id替換爲產品的name
咱們能夠這麼操做
select a.id,b.name,c.name
from tb_product_chain a
inner join tb_product b on a.product_id=b.id
inner join tb_product c on a.parent_product_join=c.id
發現結果是
1 手機 電腦
2 手機 筆記本
達到了預期效果。
一下是個人項目中的實際操做數據
tb_product_chain
tb_product
v_product_chain