我有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
----------------------------------------------------
subcategory_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.subcategory_id
和
temp_products.productid和products.product_id
和
products.product_id和product_images.product_id
是相关的..
多个产品可以属于子类别。
我希望选择一个子类别,其中包含1个图像(第一个图像)的所有产品,并且WHERE子类别.status是"启用" ... ?? (限制1)
如何在Random中选择它...... ??? (RAND)
我需要获得如下结果:
----------------------------------------------------------------------------------
subcategory_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
希望有人可以帮助我......提前致谢。
答案 0 :(得分:0)
这个未经测试的查询应该会给你结果:
Select * from subcategory join temp_products on temp_products.catid = subcategory.subcategory_id join products on temp_products.productid = products.product_id where subcategory.status='Enabled'
Buzt我不解释这部分“我怎样才能在随机选择...... ???(兰德)”这个问题的意思。
答案 1 :(得分:0)
SELECT products.*, (SELECT subcategory.id FROM subcategory WHERE `status` = enabled ORDER BY RAND() LIMIT 1) as sub
FROM products
INNER JOIN temp_products ON products.products_id = temp_products.productid
INNER JOIN subcategory ON temp_products.catid = subcategory = subcategory_id
WHERE temp_products.catid = sub
试试这个