我遇到了一个mysql查询,我希望你们能破解它...... 好的,所以我的查询选择所有特色和活动的产品并在首页上显示它们... 但是当产品有多个图像时会出现问题。这些产品在头版重复。
正如您在结果中看到的那样,最后两个产品是相同的,但重复,因为他分配了两张图片。我想知道,什么是正确的方法来只回溯一个该产品的随机图片
TNX! ;)
这是我的MySql查询:
SELECT prod.id_product, des.name, des.description,
price.price_net, price.discount_price,
prod.url, prod.active, prodpic.id_product, prodpic.id_image,
img.img_path, thumb.thumb_path
FROM products AS prod
JOIN descriptions AS des
ON prod.id_description = des.id_description
JOIN prices AS price
ON prod.id_price = price.id_price
JOIN products_pictures AS prodpic
ON prod.id_product = prodpic.id_product
JOIN product_images AS img
ON prodpic.id_image = img.id_image
JOIN product_thumb AS thumb
ON img.id_product_thumb = thumb.id_product_thumb
WHERE prod.featured = 1
AND prod.active = 1
结果是:
0 =>
object(stdClass)[21]
public 'id_product' => string '1285' (length=4)
public 'name' => string '1 item' (length=23)
public 'description' => string 'Firs item DESC.' (length=109)
public 'price_net' => string '32.786890' (length=9)
public 'discount_price' => null
public 'url' => string 'tunika-crazy-dots-white' (length=23)
public 'active' => string '1' (length=1)
public 'id_image' => string '1285' (length=4)
public 'img_path' => string 'images/webshop/products/Tunika_Crazy_Dot_527fb1f252ccd.jpg' (length=58)
public 'thumb_path' => string 'images/webshop/products/resized/Tunika_Crazy_Dot_527fb1f252ccd.jpg' (length=66)
1 =>
object(stdClass)[22]
public 'id_product' => string '1300' (length=4)
public 'name' => string 'Trol face 1' (length=11)
public 'description' => string 'Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... ' (length=112)
public 'price_net' => string '81.967213' (length=9)
public 'discount_price' => string '50' (length=2)
public 'url' => string 'Trol-face-1' (length=11)
public 'active' => string '1' (length=1)
public 'id_image' => string '1344' (length=4)
public 'img_path' => string 'images/webshop/products/Trollface1.jpg' (length=38)
public 'thumb_path' => string 'images/webshop/products/resized/Trollface1_thumb.jpg' (length=52)
2 =>
object(stdClass)[23]
public 'id_product' => string '1300' (length=4)
public 'name' => string 'Trol face 1' (length=11)
public 'description' => string 'Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... Troll face 1... ' (length=112)
public 'price_net' => string '81.967213' (length=9)
public 'discount_price' => string '50' (length=2)
public 'url' => string 'Trol-face-1' (length=11)
public 'active' => string '1' (length=1)
public 'id_image' => string '1341' (length=4)
public 'img_path' => string 'images/webshop/products/Trollface.jpg' (length=37)
public 'thumb_path' => string 'images/webshop/products/resized/Trollface_thumb.jpg' (length=51)
答案 0 :(得分:3)
你可以尝试
ORDER BY RAND()
和
GROUP BY id_image
答案 1 :(得分:1)
正如Supun Silva提到的答案很简单:)
查询是:
SELECT prod.id_product, des.name, des.description,
price.price_net, price.discount_price,
prod.url, prod.active, prodpic.id_product, prodpic.id_image,
img.img_path, thumb.thumb_path
FROM products AS prod
JOIN descriptions AS des
ON prod.id_description = des.id_description
JOIN prices AS price
ON prod.id_price = price.id_price
JOIN products_pictures AS prodpic
ON prod.id_product = prodpic.id_product
JOIN product_images AS img
ON prodpic.id_image = img.id_image
JOIN product_thumb AS thumb
ON img.id_product_thumb = thumb.id_product_thumb
WHERE prod.featured = 1
AND prod.active = 1
GROUP BY prod.id_product