Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot do this through a query by and of itself.</p> <p>Look here for some example code: <a href="https://stackoverflow.com/questions/19021631/how-to-merge-data-from-2-tables-with-mysql/19022843#19022843">How to merge data from 2 tables with MySQL</a></p> <p>What you can do is use an <code>INNER JOIN</code> or <code>GROUP_CONCAT</code> and then reformat the data in your script (whether that be <code>php</code> or another language)</p> <h3>Using JOIN</h3> <p><em>Unser the circumstances this would likely lead to a large excess/irrelevant data in each row</em></p> <pre><code>SELECT c.*, csa.answer FROM customers c INNER JOIN customer_survey_answer csa ON csa.`customer_id`=c.`customer_id` ORDER BY c.customer_id, csa.customer_survey_question_id </code></pre> <h3>Using GROUP_CONCAT</h3> <p><em>This will output one cell (csv style) as the answers</em></p> <pre><code>SELECT c.*, GROUP_CONCAT(csa.answer) as answers FROM customers c INNER JOIN customer_survey_answer csa ON csa.`customer_id`=c.`customer_id` GROUP BY c.customer_id </code></pre> <h2>Using a loop</h2> <p>You might also consider querying the database for a list of customers with answers and then running a second query (for each customer returned) to get their answer. <em>This could lead to a large number of queries.</em></p> <p>First query:</p> <pre><code>SELECT * FROM customers </code></pre> <p>Second query:</p> <pre><code>SELECT answer FROM customer_survey_answer WHERE customer_id = INSERT_CUSTOMER_ID_HERE} </code></pre>
    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.
    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