PHP:MY​​SQLI按样式更改顺序

时间:2017-07-06 07:29:05

标签: php mysql mysqli

enter image description here

嗨,我想从chat表中获取数据:

6
7
8

我的代码显示如下:

8
7
6

代码:

"SELECT * FROM `chat` WHERE 
    `chat-code` = 'vm1mxo3dpi9gzuo' AND (`user_1` = '1' OR `user_2` = '1') 
     ORDER BY id DESC LIMIT 3"

7 个答案:

答案 0 :(得分:0)

{{1}}

尝试以上查询。

答案 1 :(得分:0)

因为您按降序排序结果。您应该删除ORDER BY中的 desc 。您可以明确地将ASC指示为升序。

答案 2 :(得分:0)

您只需将查询更改为此(按文字ASC排序):

SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
(`user_1` = '1' OR `user_2` = '1')
ORDER BY text ASC LIMIT 3

如果您仍想按ID订购,只需将DESC更改为ASC

即可

答案 3 :(得分:0)

尝试阅读使用DESC或ASC语句时会发生什么。 https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html 希望它会对你有所帮助。

答案 4 :(得分:0)

使用此垫子,如果不起作用,您可以使用准备好的statements.comment更改介于参数之间。

"SELECT * FROM `chat` WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND (`user_1`='1' OR `user_2` = '1') 
AND `id` between 6 and 10  //starting and ending offset for query so query doesn't take long time
ORDER BY id ASC LIMIT 3" // LIMIT 3 as you mentioned and ASC order 

答案 5 :(得分:0)

我有你的问题的解决方案 - 使用以下查询:

SELECT * FROM (
    SELECT * FROM `chat`
    WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
    (`user_1` = '1' OR `user_2` = '1')
    ORDER BY `text` DESC LIMIT 3
) t ORDER BY `text` ASC

答案 6 :(得分:0)

SELECT a.*
  FROM 
     ( SELECT * 
         FROM chat 
        WHERE `chat-code` = 'vm1mxo3dpi9gzuo' 
          AND 1 IN(user_1,user_2) 
        ORDER 
           BY id DESC 
        LIMIT 3
     ) a
 ORDER 
    BY id;