繁体   English   中英

MySQL选择组连接?

[英]MySQL select group concat?

我正在尝试从两个表中进行选择。 我有两个表:

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

我需要获取 id(来自 igw41_users)、用户名 igw41_users、postal_code(来自 igw41_user_profiles)和 account_type(来自 igw41_user_profiles)。 但是我需要的信息在每个 igw41_user_profiles.user_id 匹配 igw41_users.id 的 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' 
group by username;

这给了我 account_type,但我不知道如何获得 postal_code 如果我正确加入,它会给我:错误 1066(42000):不是唯一的表/别名:'igw41_user_profiles'

这就是我试图获得两个配置文件值的方法:

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;

提前致谢。

从您的第一个查询中取出和 igw41_user_profiles.profile_key = 'profile.account_type'

如果你想测试两者的使用

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

通常,当您需要多次加入同一个表时,您需要为每个加入使用别名:

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;

在这种情况下,第二个连接是多余的:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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