简体   繁体   English

从两个表中选择数据。 一张桌子是父母/孩子,我需要让父母没有关系

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

I have two tables 我有两张桌子

products 制品

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

categories 类别

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

I need to pull each product and its related category AND that categories related parent based solely on the id of the product.... 我需要根据产品的ID来提取每个产品及其相关类别和类别相关的父母....

I have tried a couple of variations but can not quite get the parent categories into the fetch. 我尝试了几种变体,但无法将父类别放入fetch中。 There is no relationship for parent categories in the products table. 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.

相关问题 如何从涉及两个表的查询中获取单行。 子表的一项中重复使用2个外键 - How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table 我需要更新连接两个不同表的表。 此查询合适吗? - I need to update a table joining two different tables. Is this query the appropriate one? MySQL查询根据来自两个不同表的值选择一个表。 - MySQL query to select one table based on values from two different tables. MYSQL:我有两个表。 我想在表 1 中使用 FK 显示表 1 和表 2 中的数据 - MYSQL: I have two tables. I want to show data from table 1 and table 2 using FK in table 1 一个从两个MySQL表中选择数据的语句。 显示名称而不是ID - One statement to select data from two MySQL tables. Show name instead of/by ID 如何从二叉树表中获取带有父子关系的数据? - How get data with parent child relation from a binary tree table? 我只想要桌子上的一行。 进行JOIN还是只进行两个选择调用比较好 - I only want one row from my tables. Is it better to do a JOIN or just make two select calls 如何从父表的两个子表中获取值 - how to get values from two child tables for parent table MySQL过程将数据从登台表加载到其他表。 需要在过程中拆分多值字段 - MySQL procedure to load data from staging table to other tables. Need to split up multivalue field in the process 我有两个选择查询和两个表。我需要开发第二个表,而没有第一个表中出现的重复项 - I have two select queries and two tables.I need to develop the second table without duplicates that will be present from the first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM