簡體   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