简体   繁体   English

具有多个联接的多个mysql表

[英]multiple mysql tables with multiple joins

I have 4 mysql tables as follows: 我有4个mysql表,如下所示:

products:
----------------------------------------------------
product_id    product_name   price    discount
----------------------------------------------------
1             product 1      10.00       1.00
2             product 2      20.00       2.00
3             product 3      25.00       1.00
----------------------------------------------------

subcategory

----------------------------------------------------
cb_category_id    subcategory_name       status
----------------------------------------------------
   1                 subcat 1            Enabled
   2                 subcat 2            Disabled
   3                 subcat 3            Enabled
------------------------------------------------------

temp_products
------------------------------------------------------
id               productid               catid
------------------------------------------------------
1                   1                      1
2                   1                      2
3                   2                      1
------------------------------------------------------

product_images
------------------------------------------------------
product_id                images
------------------------------------------------------
   1                    image1.jpg
   1                    image2.jpg
   2                    image2-1.jpg
--------------------------------------------------------

temp_products.catid and subcategory.cb_category_id temp_products.catid和subcategory.cb_category_id

and

temp_products.productid and products.product_id temp_products.productid和products.product_id

and

products.product_id and product_images.product_id products.product_id和product_images.product_id

are related..A product can have multiple images. 相关。一个产品可以有多个图像。

I wish to have a subcategory selected with all products with first image for a product from product_images and WHERE subcategory.status is "Enabled"...?? 我希望从product_images和WHERE subcategory.status为“ Enabled”的所有产品中选择具有第一张图片的所有产品的子类别。 Need to limit output to only 1 cb_category_id with multiple product_id under it, like as follows: 需要将输出限制为仅1个cb_category_id,并且其下具有多个product_id,如下所示:

----------------------------------------------------------------------------------
cb_category_id   subcategory_name product_id  product_name, price, discount, images
-----------------------------------------------------------------------------------
     1              subcat 1           1        product 1    10.00   1.00    image1.jpg
     1              subcat 1           2        product 2    20.00   2.00    image2- 1.jpg

My query is as follows: 我的查询如下:

SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name 
FROM products p,subcategory s
INNER JOIN temp_products ON p.product_id = temp_products.productid
INNER JOIN temp_products tp ON tp.catid = s.cb_category_id
WHERE tp.catid = s.cb_category_id        

I am getting unknown column p.product_id in on clause ....Regarding including images i am at a dead end. 我在on子句中获得了未知的列p.product_id ....关于包括图像,我处在一个死胡同。 Help requested...I am unable to comprehend the joins required for the same... 需要帮助...我无法理解相同要求的联接...

you have a typo . 你有错字。 you dont have products_id in your products table. 您的产品表中没有products_id。

change this 改变这个

  p.products_id

to

  p.product_id

EDIT: rewrite your query like that : 编辑:像这样重写您的查询:

  SELECT  p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name ,pi.images
  FROM products p
  INNER JOIN temp_products tp ON p.product_id = tp.productid
  INNER JOIN product_images pi ON p.product_id = pi.product_id
  INNER JOIN subcategory s ON tp.catid = s.cb_category_id
  GROUP BY p.product_id 

Your 'temp_products' table is basically a mapping table for 'products' and 'subcategory' . 您的'temp_products'表基本上是'products' and 'subcategory'的映射表。 I have not tested the following code but you'll get the idea from comments(text after # character). 我尚未测试以下代码,但您将从注释(#字符后的文本)中得到启发。 this should solve your problem: 这应该可以解决您的问题:

SELECT p.product_id,p.product_name,p.price,p.discount,s.cb_category_id,s.subcategory_name,pim.images
FROM products p
INNER JOIN temp_products tp ON p.product_id = tp.productid          #remove duplicate join
INNER JOIN subcategory s ON tp.catid = s.cb_category_id
LEFT JOIN product_images pim ON pim.product_id =p.product_id        #join for images
WHERE s.status = 'Enabled' AND                                      #subcategory is enabled
s.cb_category_id = '1'                                              #subcategory you want to be selected
GROUP BY p.product_id ORDER BY p.product_id, pim.images;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM