简体   繁体   中英

MySQL select group concat?

I am trying to do a select from two tables. I have two tables:

select * from igw41_users;
+-----+------------+----------+-----------------------------+--------------------------------------------------------------+-------+-----------+---------------------+---------------------+------------+--------------------------------------------------------------------------------------------------------------------------------+---------------------+------------+--------+------+--------------+
| id  | name       | username | email                       | password                                                     | block | sendEmail | registerDate        | lastvisitDate       | activation | params                                                                                                                         | lastResetTime       | resetCount | otpKey | otep | requireReset |
  17      John Doe     AAAAAA   nothing else is needed from this table.

select * from igw41_user_profiles;
+---------+----------------------+-----------------------+----------+
| user_id | profile_key          | profile_value         | ordering |
   17      profile.account_type    "2"                      4
   17      profile.postal_code     "75055"                  1

I need to get id (from igw41_users), username igw41_users , postal_code (from igw41_user_profiles) and account_type (from igw41_user_profiles). But the info I need is in profile_values for each igw41_user_profiles.user_id that matches igw41_users.id.

select id, username, GROUP_CONCAT(profile_value SEPARATOR ',') 
from igw41_users 
    left join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.account_type' 
group by username;

this gives me the account_type, but I can't figure out how to get the postal_code If I do a right join it gives me: ERROR 1066 (42000): Not unique table/alias: 'igw41_user_profiles'

This is what I tried to get both profile values:

select id, username, GROUP_CONCAT(profile_value SEPARATOR ',') 
from igw41_users 
    left join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.account_type' 
    right join igw41_user_profiles on igw41_users.id = igw41_user_profiles.user_id and igw41_user_profiles.profile_key = 'profile.postal_code' 
group by username;

Thanks in advance.

take and igw41_user_profiles.profile_key = 'profile.account_type' out of your first query

If you wish to test for both use in

and igw41_user_profiles.profile_key in('profile.account_type','profile.postal_code')

In common way when you need to join same table more then once you need to use aliases for each join:

select id, username, GROUP_CONCAT(profile_value SEPARATOR ',') 
from igw41_users 
left join igw41_user_profiles p1
    on igw41_users.id = p1.user_id and 
       p1.profile_key = 'profile.account_type' 
right join igw41_user_profiles p2
    on igw41_users.id = p2.user_id and
       p2.profile_key = 'profile.postal_code' 
group by username;

In this case the second join is redundant:

select id, username, GROUP_CONCAT(profile_value SEPARATOR ',') 
from igw41_users 
left join igw41_user_profiles p1 on igw41_users.id = p1.user_id
where
    p1.profile_key = 'profile.account_type' OR 
    p1.profile_key = 'profile.postal_code'
group by username;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM