SELECT GROUP BY子句,包含非聚合列

时间:2019-09-06 15:32:37

标签: mysql sql

我正在为我的应用程序设置一个消息传递组件,我有一个存储过程,它将从数据库中检索最新的对话。需要从每个会话中获取消息文本以及对方用户的缩略图。 我尝试了其他解决方案,例如从sql_mode中删除“ only_full_group_by”,但是没有运气。有没有其他方法可以使该查询正常工作?

DELIMITER $$
USE `myDatabase`$$
DROP PROCEDURE IF EXISTS `myDatabase`.`sp_GetConversations` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_GetConversations`(
    IN p_userId bigint
)
BEGIN
    IF (
        SELECT EXISTS (
            SELECT 1 
                FROM conversations A
                    LEFT JOIN users B
                        ON (B.userId = A.userId1 OR B.userId = A.userId2)
            WHERE 
                CASE 
                    WHEN A.userId1 = p_userId THEN A.userId2 = B.userId
                    WHEN A.userId2 = p_userId THEN A.userId1 = B.userId
                END
        ) 
    ) THEN
        SELECT
            "success" AS result,
            A.id AS a_id,
            A.userId1 AS a_userId1,
            A.userId2 AS a_userId2,
            B.conversation AS b_conversation,
            B.body AS b_body,
            B.created AS b_created,
            C.username AS c_username,
            sp_GetConversationsThumbnail(p_userId, A.id) AS thumbnail
        FROM 
            conversations A                  
                LEFT JOIN messages B 
                    ON B.conversation = A.id
                LEFT JOIN users C 
                    ON (C.userId = A.userId1 OR C.userId = A.userId2)
        WHERE 
            CASE 
                WHEN A.userId1 = p_userId THEN A.userId2 = C.userId
                WHEN A.userId2 = p_userId THEN A.userId1 = C.userId
            END 
        AND B.created = (
                    SELECT MAX(created)
                    FROM messages AS Z
                    WHERE Z.conversation = A.id
                )
        GROUP BY B.conversation
        ORDER BY A.id
        DESC 
        Limit 20;
    ELSE
        SELECT 'error' AS result;
    END IF;
END$$
DELIMITER ;

会话表:

+----+---------+---------+---------------------+
| id | userId1 | userId2 | created             |
+----+---------+---------+---------------------+
| 1  |   2     |    3    | 2019-02-24 17:25:18 |
+----+---------+---------+---------------------+
| 2  |   3     |    6    | 2019-03-12 18:14:00 |
+----+---------+---------+---------------------+

消息表:

+-----+--------------+--------+-----------+---------------+------+---------------------+
| id  | conversation | sender | recipient | body          | read | created             | 
+-----+--------------+--------+-----------+---------------+------+---------------------+
| 1   | 1            | 2      | 3         | lorem ipsum 1 | 0    | 2019-06-13 17:35:39 | 
+-----+--------------+--------+-----------+---------------+------+---------------------+
| 2   | 1            | 3      | 2         | lorem ipsum 2 | 0    | 2019-06-13 21:54:56 | 
+-----+--------------+--------+-----------+---------------+------+---------------------+
| 3   | 2            | 6      | 3         | lorem ipsum 3 | 0    | 2019-06-13 23:22:45 | 
+-----+--------------+--------+-----------+---------------+------+---------------------+

MySql错误:

  

SELECT列表的表达式#5不在GROUP BY子句中,并且包含未聚合的列“ myDatabase.B.body”,该列在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by

不兼容

0 个答案:

没有答案