8

Say I have this data set

  user  | group
--------+-------
[email protected] |   A
[email protected] |   B
[email protected] |   A
[email protected] |   B
[email protected] |   A
[email protected] |   B
[email protected] |   C

I want to convert this into a table like this:

  user  | IN_A  | IN_B  | IN_C
--------+-------+-------+-------
[email protected] | TRUE  | TRUE  | FALSE
[email protected] | TRUE  | FALSE | FALSE
[email protected] | FALSE | TRUE  | FALSE
[email protected] | TRUE  | TRUE  | TRUE

I've got:

SELECT
  user,
  IF(LOCATE('A', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_A,
  IF(LOCATE('B', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_B,
  IF(LOCATE('C', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_C
FROM users
GROUP BY user

What I'm wondering is if there is a better way to known if an aggregated field contains a value, or if this is the only way?

5 Answers 5

20

The right way to do it:

SELECT
   user,
   IF(SUM(group = 'A'), TRUE, FALSE) AS IN_A,
   IF(SUM(group = 'B'), TRUE, FALSE) AS IN_B,
   IF(SUM(group = 'C'), TRUE, FALSE) AS IN_C
FROM users
GROUP BY user
1
1

I guess this is the only way i.e., GROUP_CONCAT

1
  • The only other way I've found is SELECT u.user, u1.group, g2.group FROM users u LEFT JOIN users u1 ON u.user = u1.user AND u1.group = "A" LEFT JOIN users u2 ON u.user = u2.user AND u2.group = "B" GROUP BY u.user;
    – Nick
    Sep 16, 2010 at 13:25
1

You can try something like this piece:

SELECT distinct user,  
IF(EXISTS
    (SELECT users_a.user 
        from users as users_a 
    where users_a.group = 'A' and
        users_a.user = users.user), 
TRUE, FALSE) as IN_A,
IF(EXISTS
    (SELECT users_b.user 
        from users as users_b 
    where users_b.group = 'B' and
        users_b.user = users.user), 
TRUE, FALSE) as IN_B,
IF(EXISTS
    (SELECT users_c.user 
        from users as users_c 
    where users_c.group = 'C' and
        users_c.user = users.user), 
TRUE, FALSE) as IN_C
FROM `users` WHERE 1

Tryed here works fine!

0

Not sure how many groups you will be having but if its a finiate number I would suggest that you create a column for each group or use the mysql data type SET (bit masking).

1
  • It is finite - I am trying to move from a dual-key (user/group) system to a single key (user) system with each group having its own column.
    – Nick
    Sep 16, 2010 at 13:23
0

I'm not sure if this is the way you do things (as I'm quite new here) but if that's the example you're using wouldn't it be easier to have the columns user, IN_A, IN_B and IN_C? Especially since that way you wouldn't be repeating the user data again

1
  • It would - thats what I'm trying to achieve :) The Data Set uses User and Group as a double-column PRIMARY KEY. I want to may User the PRIMARY KEY and split the Groups into columns.
    – Nick
    Sep 16, 2010 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.