LEFT JOIN GROUP结果与PK相同

时间:2017-10-20 01:29:39

标签: php mysql group-by left-join

我想在我的查询中使用另一个GROUP LEFT JOIN行,如下所示:

   [ID] => 1,
   [user_email] => 'example@example.com',
   [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  | example@org.com  |2017-10-13 14:39:20    |
--------------------------------------------------
|  3  | example2@org.com |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] => example@org.com
            [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] => example@org.com
            [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)。如何获得第一个结果示例?我想在一个查询中做,而不是使用foreach(PHP)。请有人可以帮帮我吗?最好的问候。

1 个答案:

答案 0 :(得分:0)

在MySQL 5.7中,您可以使用SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));覆盖默认的ONLY_FULL_GROUP_BY sqlmode,在聚合列上启用GROUP BY,如Error Code: 1055 incompatible with sql_mode=only_full_group_by

中所述