![](/img/trans.png)
[英]How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table
[英]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.