Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL GROUP CONCAT duplicates
    primarykey
    data
    text
    <p>I have a table of results that looks like this:</p> <pre><code>score_id, uid, af_id, level, record_date </code></pre> <p>Where <code>uid</code> is the user id of a user on the system. <code>af_id</code> is a focus which a level has been set at. An <code>af_id</code> will be either 1.1, 1.2, 1.3. <code>Level</code> is an integer value, ranging from 3 - 7.</p> <p>A sample data set would be:</p> <pre><code>1, 2, 1.1, 4, 2012-09-02 2, 2, 1.2, 4, 2012-09-02 3, 2, 1.3, 3, 2012-09-02 4, 2, 1.3, 4, 2012-11-30 </code></pre> <p>I need to query the database to collect what level a user has reached for each <code>af_id</code> between certain dates. I am using <code>GROUP_CONCAT</code> to bring the data into one row. My query works for the first period of time, but produces duplicates there after. My SQL so far is:</p> <pre><code>SELECT u.uid, u.forename, u.surname, ui.gender, GROUP_CONCAT(us.level ORDER BY us.af_id SEPARATOR ',') AS levels FROM users u LEFT OUTER JOIN user_scores us ON us.uid=u.uid AND us.record_date &gt; '2012-09-01' AND us.record_date &lt; '2012-10-20' JOIN user_info ui ON ui.uid =u.uid GROUP BY u.uid ORDER BY u.surname ASC </code></pre> <p>This gives the result:</p> <pre><code>Joe Bloggs M 4,4,3 </code></pre> <p>I want to be able to widen the months so that my query will be able to return updated results for af_id's. So if I widen the results dates to range between 2012-09-01 and 2012-12-31. I would like to see the following results:</p> <pre><code>Joe Bloggs M 4,4,4 </code></pre> <p>However, my query (using the above SQL) returns the following result:</p> <pre><code>Joe Bloggs M 4,4,3,4 </code></pre> <p>Any help with this would be much appreciated. Thanks in advance.</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.
    1. This table or related slice is empty.
    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