我正在使用这个mysql查询
SELECT `p`.`id` AS product_id, `p`.`title` , `i`.`image` , `u`.`user_username` , `m`.`id` AS message_id, `m`.`date` , `m`.`from` , `m`.`to` , `m`.`message` , `m`.`read`
FROM (`messages` AS m)
JOIN `products` AS p ON `m`.`product_id` = `p`.`id`
JOIN `users` AS u ON `m`.`from` = `u`.`user_id`
JOIN `product_images` AS i ON `p`.`id` = `i`.`product_id`
WHERE `i`.`type` = 'FRONT'
AND (m.to = '1171' OR m.from = '1171)
GROUP BY `m`.`therad_user` , `m`.`product_id`
ORDER BY `m`.`id` DESC
在消息表中显示消息ID最旧的消息,我想返回最新消息。但它显示了最古老的信息。我是怎么解决的。
答案 0 :(得分:1)
首先尝试ORDER BY,然后只尝试GROUP BY
SELECT * FROM
(
SELECT `p`.`id` AS product_id, `p`.`title` , `i`.`image` , `u`.`user_username` , `m`.`id` AS message_id, `m`.`date` , `m`.`from` , `m`.`to` , `m`.`message` , `m`.`read`
FROM (`messages` AS m)
JOIN `products` AS p ON `m`.`product_id` = `p`.`id`
JOIN `users` AS u ON `m`.`from` = `u`.`user_id`
JOIN `product_images` AS i ON `p`.`id` = `i`.`product_id`
WHERE `i`.`type` = 'FRONT'
AND (m.to = '1171' OR m.from = '1171)
ORDER BY `m`.`id` DESC
) tmp
GROUP BY `m`.`therad_user` , `m`.`product_id`