Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving duplicate answers when query a database
    primarykey
    data
    text
    <p>ok I have looked around here for a few days now, and I did find this : <a href="https://stackoverflow.com/questions/2183373/mysql-group-by-two-columns">MySQL GROUP BY two columns</a> This has been helpful but hasn't worked for what I am currently working on.</p> <p>So here is the scope of things I have 3 different tables</p> <p>table 1 (quantities) consist of:</p> <pre><code>quantity_id int(11) auto_increment not null (primary key) product_id int(11) not null color_id int(11) not null size_id int (11) not null quantity int (11) not null </code></pre> <p>table 2 (colors) consist of:</p> <pre><code>color_id int(11) auto_increment not null (primary key) color_name varchar(255) </code></pre> <p>table 3 (sizes) consist of:</p> <pre><code>size_id int (11) auto_increment not null (primary key) size varchar(255) SELECT s.size, c.color_name, q.quantity, q.size_id, q.product_id, q.color_id FROM quantities q, colors c, sizes s WHERE q.color_id = c.color_id AND q.size_id = s.size_id AND q.product_id = $pid GROUP BY q.color_id ORDER BY q.size_id </code></pre> <p>$pid is the product id passed by the form.</p> <p>When I load the results into 2 different select statements: one for color, one for size I get multiple of the same color or size:</p> <pre><code>red s red L yellow L </code></pre> <p>I understand that it is literally pulling the rows out of the database and displaying them, but how do I not repeat myself?</p> <p>I have tried this query</p> <pre><code>SELECT s.size, c.color_name, q.quantity, q.size_id, q.product_id, q.color_id FROM quantities q LEFT JOIN colors c ON q.color_id = c.color_id LEFT JOIN sizes s ON q.size_id = s.size_id WHERE q.product_id = $pid </code></pre> <p>Is there a better way?</p>
    singulars
    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.
 

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