2013-10-02 65 views
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個結果。我附上了我錯誤結果的照片。

enter image description here 應該給我id_msg:20,19,18,17,16 ECC ..

回答

1

這是因爲沒有排序應用LIMIT 10。你應該重新安排你的查詢限制之前申請的順序,使最高id_msg S中的行會出現在十大:

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 
+0

謝了你的男人。這是正確的 – fabrizio