2

我想得到一个查询的结果,其中数字 id 是 < 21 这个是查询:

SELECT * 
FROM (

SELECT users.name, users.surname, users.username, inbox_msg.message, inbox_msg.id_msg, inbox_msg.occured_at, image_upload.name_image
FROM inbox_msg
INNER JOIN users ON users.id = inbox_msg.id_user
INNER JOIN image_upload ON image_upload.id_image = users.profile_image
WHERE inbox_msg.id_conversation = 1
AND inbox_msg.id_msg < 21
LIMIT 10
)mex
ORDER BY occured_at ASC

我不知道为什么获得的结果是我表的前 10 个结果。我附上了我错误结果的图片。

在此处输入图像描述 应该给我 id_msg: 20,19,18,17,16 ecc..

4

1 回答 1

1

这是因为在LIMIT 10没有排序的情况下应用了。您应该重新安排查询以在限制之前应用排序,以便具有最高id_msgs 的行将出现在前十名中:

SELECT * 
FROM (
    SELECT users.name, users.surname, users.username, inbox_msg.message, inbox_msg.id_msg, inbox_msg.occured_at, image_upload.name_image
    FROM inbox_msg
    INNER JOIN users ON users.id = inbox_msg.id_user
    INNER JOIN image_upload ON image_upload.id_image = users.profile_image
    WHERE inbox_msg.id_conversation = 1
    AND inbox_msg.id_msg < 21
    ORDER BY id_msg DESC -- <<== Add this line
    LIMIT 10
)mex
ORDER BY occured_at ASC
于 2013-10-02T16:29:51.427 回答