簡體   English   中英

從兩個表中選擇數據。 一張桌子是父母/孩子,我需要讓父母沒有關系

[英]Select data from two tables. one table is parent/child and I need to get the parents without a relation

我有兩張桌子

制品

id  |  catID  |  UID
--------------------
1   |  3      |  3

類別

id  |  cat_name  |  parent
--------------------------
2   |  XYZ       |  0
3   |  abc       |  2

我需要根據產品的ID來提取每個產品及其相關類別和類別相關的父母....

我嘗試了幾種變體,但無法將父類別放入fetch中。 product表中的父類別沒有關系。

SELECT
  a.product_name, a.catID, b.cat_name, b.parent
FROM
  products a, categories b
WHERE
  a.id = '$_SESSION[spid]' 
  AND b.id = a.catID
  OR b.id = b.parent /// not correct..
select p.*,c1.cat_name as category, c2.cat_name as parent_category
from products p
left join categories c1 on (c1.id=p.catId)
left join categories c2 on (c1.parent=c2.id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM