【问题标题】:MySQL - select only one (random) image of manyMySQL - 仅选择许多图像中的一个(随机)图像
【发布时间】:2013-12-14 13:34:00
【问题描述】:

我被一个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)

【问题讨论】:

    标签: php mysql distinct var-dump


    【解决方案1】:

    你可以试试

    ORDER BY RAND()
    

    GROUP BY id_image
    

    【讨论】:

    • 不知道你是否正确理解了问题...我希望mysql选择beatween $product[1] OR $product[2]并且只输出一个,因为这是同一个产品有两张图片...
    【解决方案2】:

    正如 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
    

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 1970-01-01
      • 2014-01-28
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多