2017-10-20 134 views
0

GROUP結果我想GROUP LEFT JOIN排在我的查詢,另一種,就像這樣:LEFT JOIN與同一PK

[ID] => 1, 
    [user_email] => '[email protected]', 
    [user_registered] => '2017-10-13 14:39:20', 
    [preferences_id] => 1, 
    [uid] => 1, 
    [user_social_links] => Array 
     (
      [1 *social_id index*] => Array 
       (
        [name] => FACEBOOK 
        [value] => http://facebook.com/url 
        [display] => 1 
       ) 

      [2 *social_id index*] => Array 
       (
        [name] => TWITTER 
        [value] => http://twitter.com/url 
        [display] => 1 
       ) 

     ) 

我有這些表:

  • 用戶
  • USER_ACCOUNT
  • user_social_links
 
users : 
-------------------------------------------------- 
| ID | user_email  | user_registered  | 
-------------------------------------------------- 
| 1 | [email protected] |2017-10-13 14:39:20 | 
-------------------------------------------------- 
| 3 | [email protected] |2017-10-13 15:20:05 | 
-------------------------------------------------- 
user_account: 
-------------------------------------------------- 
| preferences_id | uid | user_nicename   | 
-------------------------------------------------- 
| 1    | 1 | example nice name1  | 
-------------------------------------------------- 
| 2    | 3 | example nice name2  | 
-------------------------------------------------- 

user_social_links : 
------------------------------------------------------------ 
| social_id | uid | name  | value    | display | 
------------------------------------------------------------ 
| 1  | 1 | FACEBOOK | fb.me/example  | 1  | 
------------------------------------------------------------ 
| 2  | 1 | TWITTER | twitter.com/example| 1  | 
------------------------------------------------------------ 

我的查詢是:

SELECT 
user_account.preferences_id,user_account.uid,user_account.user_nicename, 
users.ID,users.user_email,users.user_registered,user_social_links.social_id,user_social_links.value,user_social_links.name,user_social_links.display, 
FROM user_account 
LEFT JOIN user_social_links 
    ON user_social_links.uid = user_account.uid 
LEFT JOIN users 
    ON users.ID = user_account.uid 
WHERE user_account.uid = :userid 

輸出:

Array 
(
    [0] => Array 
     (
      [preferences_id] => 1 
      [uid] => 1 
      [user_nicename] => example nice name1 
      [ID] => 1 
      [user_email] => [email protected] 
      [user_registered] => 2017-10-13 14:39:20 
      [social_id] => 1 
      [value] => fb.me/example 
      [name] => FACEBOOK 
      [display] => 1 
     ) 

    [1] => Array 
     (
      [preferences_id] => 1 
      [uid] => 1 
      [user_nicename] => example nice name1 
      [ID] => 1 
      [user_email] => [email protected] 
      [user_registered] => 2017-10-13 14:39:20 
      [social_id] => 2 
      [value] => twitter.com/example 
      [name] => TWITTER 
      [display] => 1 
     ) 

) 

注:我不能使用GROUP BY user_account.uid(#1055 only_full_group_by錯誤的MySQL).¿How我可以得到第一個結果例?我想在一個查詢中做,而不是使用foreach(PHP)。請任何人都可以幫助我?最好的祝福。

回答

0

在MySQL 5.7,你可以在彙總列使GROUP BY通過重寫使用SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));默認ONLY_FULL_GROUP_BY sqlmode截至Error Code: 1055 incompatible with sql_mode=only_full_group_by

+0

我readed有關解釋,但我會更喜歡一些方式,而不設定的sql_mode做的,你知道其他方式?感謝您的回覆。 – elaineee