title: MySQL - select only one (random) image of many
body: <p>I got stucked with one mysql query and i hope you guys can crack it...
  Ok, so my query selects all products witch are featured and active and displays them on front page...
  But problems appears when product has more then one image. This products are duplicated then on front page.</p>
  
  <p>As you can see in results, last two products are the same, but duplicated because he has two pictures assigned on it. I whould like to know, what is the right way to retrive only one random picture of this product</p>
  
  <p>Tnx! ;)</p>
  
  <p>Here is my MySql query:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And the result is:</p>
  
  <pre><code>      0 =&gt; 
      object(stdClass)[21]
        public 'id_product' =&gt; string '1285' (length=4)
        public 'name' =&gt; string '1 item' (length=23)
        public 'description' =&gt; string 'Firs item DESC.' (length=109)
        public 'price_net' =&gt; string '32.786890' (length=9)
        public 'discount_price' =&gt; null
        public 'url' =&gt; string 'tunika-crazy-dots-white' (length=23)
        public 'active' =&gt; string '1' (length=1)
        public 'id_image' =&gt; string '1285' (length=4)
        public 'img_path' =&gt; string 'images/webshop/products/Tunika_Crazy_Dot_527fb1f252ccd.jpg' (length=58)
        public 'thumb_path' =&gt; string 'images/webshop/products/resized/Tunika_Crazy_Dot_527fb1f252ccd.jpg' (length=66)
    1 =&gt; 
      object(stdClass)[22]
        public 'id_product' =&gt; string '1300' (length=4)
        public 'name' =&gt; string 'Trol face 1' (length=11)
        public 'description' =&gt; 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' =&gt; string '81.967213' (length=9)
        public 'discount_price' =&gt; string '50' (length=2)
        public 'url' =&gt; string 'Trol-face-1' (length=11)
        public 'active' =&gt; string '1' (length=1)
        public 'id_image' =&gt; string '1344' (length=4)
        public 'img_path' =&gt; string 'images/webshop/products/Trollface1.jpg' (length=38)
        public 'thumb_path' =&gt; string 'images/webshop/products/resized/Trollface1_thumb.jpg' (length=52)
    2 =&gt; 
      object(stdClass)[23]
        public 'id_product' =&gt; string '1300' (length=4)
        public 'name' =&gt; string 'Trol face 1' (length=11)
        public 'description' =&gt; 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' =&gt; string '81.967213' (length=9)
        public 'discount_price' =&gt; string '50' (length=2)
        public 'url' =&gt; string 'Trol-face-1' (length=11)
        public 'active' =&gt; string '1' (length=1)
        public 'id_image' =&gt; string '1341' (length=4)
        public 'img_path' =&gt; string 'images/webshop/products/Trollface.jpg' (length=37)
        public 'thumb_path' =&gt; string 'images/webshop/products/resized/Trollface_thumb.jpg' (length=51)
  </code></pre>