Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of your problems is that you are grouping by <code>mark_id</code>, which will mean you end up with multiple rows per product, and not all marks in each row, however this is only hiding another problem. Because you have multiple one to many relationships you are getting cartesian products in your join. Imagine the following simple example for prod_id = 1</p> <p><strong>Images</strong></p> <pre><code>pid url -------------- 1 some_url 1 some_other_url </code></pre> <p><strong>Marks</strong></p> <pre><code>pid mark ------------------- 1 1 1 2 </code></pre> <p>When you join these two on product ID you end up with</p> <pre><code>pid url mark ------------------------------ 1 some_url 1 1 some_url 2 1 some_other_url 1 1 some_other_url 2 </code></pre> <p>So you get all combinations of the two, so when you do GROUP_CONCAT on these you will get duplicates.</p> <p>You either need to use subqueries to do your group concats:</p> <pre><code>SELECT p.prod_id, p.prod_name, p.prod_desc, p.prod_num, p.prod_marks, p.prod_minprice, p.prod_minbid, p.prod_cat, img.images, m.marks, m.mark_descriptions FROM ci_products p INNER JOIN ( SELECT i.img_pid, GROUP_CONCAT( i.img_url ) images FROM ci_prodimages i GROUP BY i.img_pid ) img ON img.img_pid = p.prod_id INNER JOIN ( SELECT ci_prodmarks.pmark_pid, GROUP_CONCAT( ci_marks.mark_mark ) marks, GROUP_CONCAT( ci_marks.mark_desc SEPARATOR ";" ) mark_descriptions FROM ci_prodmarks INNER JOIN ci_marks ON ci_prodmarks.pmark_mid = ci_marks.mark_id GROUP BY ci_prodmarks.pmark_pid ) m ON m.pmark_pid = p.prod_id WHERE p.prod_cat IN ('4', '8', '9', '10') </code></pre> <p><strong><a href="http://sqlfiddle.com/#!2/ae173/4" rel="nofollow">Example on SQL Fiddle</a></strong></p> <p>Or use DISTINCT in your GROUP_CONCAT:</p> <pre><code>SELECT p.prod_id, p.prod_name, p.prod_desc, p.prod_num, p.prod_marks, p.prod_minprice, p.prod_minbid, p.prod_cat, GROUP_CONCAT( i.img_url ) images, GROUP_CONCAT( DISTINCT m.mark_mark ) marks, GROUP_CONCAT( DISTINCT m.mark_desc SEPARATOR ";" ) mark_descriptions FROM ci_products p INNER JOIN ci_prodimages i ON i.img_pid = p.prod_id INNER JOIN ci_prodmarks pm ON pm.pmark_pid = p.prod_id INNER JOIN ci_marks m ON pm.pmark_mid = m.mark_id WHERE p.prod_cat IN ('4', '8', '9', '10') GROUP BY p.prod_id, p.prod_name, p.prod_desc, p.prod_num, p.prod_marks, p.prod_minprice, p.prod_minbid, p.prod_cat </code></pre> <p><strong><a href="http://sqlfiddle.com/#!2/ae173/2" rel="nofollow">Example on SQL Fiddle</a></strong></p> <p>N.B.</p> <p>I've also replaced this:</p> <pre><code>WHERE `prod_cat` = '4' OR `prod_cat` = '8' OR `prod_cat` = '9' OR `prod_cat` = '10' </code></pre> <p>with a single <code>IN</code> statement:</p> <pre><code>WHERE p.prod_cat IN ('4', '8', '9', '10') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload