具有多个连接的多个mysql表

时间:2014-07-03 11:27:13

标签: mysql

我有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和subcategory.cb_category_id

temp_products.productid和products.product_id

products.product_id和product_images.product_id

是相关的。产品可以有多个图像。

我希望选择一个子类别,其中所有产品都带有第一张图片,产品来自product_images,WHERE子类别.status是"已启用" ... ??需要将输出限制为仅包含多个product_id的1 cb_category_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

我的查询如下:

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        

我在on子句中得到了未知的列p.product_id ....关于包含图像我处于死胡同。 请求帮助......我无法理解同样的联接......

2 个答案:

答案 0 :(得分:2)

你有一个错字。你的产品表中没有products_id。

更改此

  p.products_id

  p.product_id

编辑:像这样重写你的查询:

  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 

答案 1 :(得分:0)

您的'temp_products'表基本上是'products' and 'subcategory'的映射表。我没有测试过以下代码,但你会从评论中获得想法(#字符后的文字)。这应该可以解决你的问题:

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;