Mysql 使用一个单查询从四个表中获取数据

Mysql 使用一个单查询从四个表中获取数据,mysql,sql,Mysql,Sql,我有四张桌子。首先让我分享一下他们的结构 子类别 id name 第三子类 id name sub_category_id 产品 id name sub_category_three_id id image product_id 图像 id name sub_category_three_id id image product_id 现在我想得到1个子类别,在子类别(id)的基础上得到子类别3的所有数据,在子类别3的基础上得到子类别3的所有产品,以及产品基础上的所有图像

我有四张桌子。首先让我分享一下他们的结构

子类别

id
name
第三子类

id
name
sub_category_id
产品

id
name
sub_category_three_id
 id
 image
 product_id
图像

id
name
sub_category_three_id
 id
 image
 product_id
现在我想得到1个子类别,在子类别(id)的基础上得到子类别3的所有数据,在子类别3的基础上得到子类别3的所有产品,以及产品基础上的所有图像

我在三个表上尝试了这个查询,但后来变得复杂了,所以我来到这里

SELECT sct.sct_name,sc_name,p.name 
  FROM sub_categories_three sct 
 INNER JOIN sub_categories sc 
    ON sc.id = sct.sub_category_id 
 INNER JOIN products p 
    ON sct.id = p.sub_category_three

这是一个简单的连接查询,用于连接所有表:

select * from sub_category sc inner join
  sub_category_three sct on sc.id = sct.sub_category_id inner join
  products p on sct.id = p.sub_category_three_id inner join 
  images i on i.product_id = p.id
  where ....

这是一个简单的内部连接查询

SELECT * FROM products 
INNER JOIN images ON images.product_id = products.id                           //link images with product id 
INNER JOIN sub category three sct ON sct.id = products.subcategory_three_id    //link product subcategory three id with sub category three 
INNER JOIN sub category sc ON sc.id = sct.sub_category_id                     //finally link sub category with subcategory three `
如有必要,添加Where条件

编辑

SELECT * FROM products 
INNER JOIN images ON images.product_id = products.id                           //link images with product id 
INNER JOIN sub category three sct ON sct.id = products.subcategory_three_id    //link product subcategory three id with sub category three 
INNER JOIN sub category sc ON sc.id = 
  (  SELECT id
       FROM sub category AS sc
        WHERE sc.id = sct.sub_category_id
        LIMIT 1

   )             //finally link sub category with subcategory three `

您可以使用以下查询,它包含一些解释:

SELECT
    -- used aliases as most of the tables have same field names.
    -- not required but it will help us distinguish between the returned values.
    -- also the query will work without aliases as we did make aliases for table names which is required to prevent ambiguous field names. 
    `sc`.`id` AS `scID`,
    `sc`.`name` AS `scName`,
    `sct`.`id` AS `sctID`,
    `sct`.`name` AS `sctName`,
    `sct`.`sub_category_id`,
    `p`.`id` AS `pID`,
    `p`.`name` AS `pName`,
    `p`.`sub_category_three_id`,
    `i`.`id` AS `iID`,
    `i`.`image`,
    `i`.`product_id`
FROM `sub_categories` `sc`
INNER JOIN `sub_categories_three` `sct` ON -- link `sub_categories` and `sub_categories_three` tables 
    `sct`.`sub_category_id` = `sc`.`id` -- based on `sub_categories`.`id` and `sub_categories_three`.`sub_category_id`.
INNER JOIN `products` `p` ON -- link `sub_categories_three` and `products` tables
    `p`.`sub_category_three_id` = `sct`.`id` -- based on `sub_categories_three`.`id` and `products`.`sub_category_three_id`.
INNER JOIN `images` `i` ON -- link `products` and `images` tables
    `i`.`product_id` = `p`.`id` -- based on `products`.`id` and `images`.`product_id`.
WHERE `sc`.`id` = 1 -- a WHERE clause is needed, for demo purpuses I fetched only the row having `id` = 1 from `sub_categories` table. Change it per your requirements.

希望这能有所帮助,请随意要求更多的澄清。

请解释一下。这很简单-如果表a的b_id列与b的id列相匹配,您可以对a.b_id=b.id进行
内部连接来连接它们。如果需要联接4个表,可以联接3次。
images
表的
product
列是
外键,用于
产品的
名称
id
表?尝试左联接或右联接。@ths是,让我编辑我的问题,因为每个产品可以有多个图像-结果应该是什么样的?你想要每张图片一行(复制产品)还是每张产品一行(汇总图片)?一张图片对应一个产品如果我只需要一个子类别名称怎么办?我使用你的第一个查询,它是return only name of products=>
选择p.name,i.image,sc.sc\u name,sct.sct\u产品名称p内部连接图片i ON i.product\u id=p.id内部连接子类\u三个sct ON sct.id=p.sub\u类别\u三个内部连接子类sc ON sc.id=sct.sub\u category\u id其中sc.id=1
是如果不检查数据,它将返回带有子类别的产品内部连接错误使用p.sub_category_three_id@AliRaza