Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL子查询中使用where子句?_Mysql_Subquery_Where Clause - Fatal编程技术网

如何在MySQL子查询中使用where子句?

如何在MySQL子查询中使用where子句?,mysql,subquery,where-clause,Mysql,Subquery,Where Clause,我使用以下MySQL查询访问聊天日志中最近的20条消息,并颠倒顺序,以便最后一次在屏幕上打印最新消息: SELECT * FROM ( SELECT messageID, posterID, messageTime, message FROM chat_messages /* Subquery is used to g

我使用以下MySQL查询访问聊天日志中最近的20条消息,并颠倒顺序,以便最后一次在屏幕上打印最新消息:

SELECT * 
FROM 
    (
        SELECT
            messageID,
            posterID,
            messageTime,
            message
        FROM
            chat_messages
        /* Subquery is used to get the most recent 20 by messageTime */
        ORDER BY 
            messageTime DESC
        LIMIT 20
    ) subq
/* Reorder the result of the subquery to put them back into ascending order */
ORDER BY 
    messageTime ASC
它工作得很好。问题是,我现在正试图将组添加到聊天功能中。在此过程中,我在chat_messages表中添加了另一列,称为“group”。主聊天日志是组0,因此我需要更改上述查询,以便仅访问主聊天日志中的消息。这就是我被困的地方。看来MySQL不允许我在子查询中添加where子句。我尝试了以下方法,但无效:

SELECT * 
FROM 
    (
        SELECT
            messageID,
            posterID,
            messageTime,
            message
        FROM
            chat_messages
        WHERE
            group = '0'
        /* Subquery is used to get the most recent 20 by messageTime */
        ORDER BY 
            messageTime DESC
        LIMIT 20
    ) subq
/* Reorder the result of the subquery to put them back into ascending order */
ORDER BY 
    messageTime ASC
我收到此错误消息(第58行是查询后的下一行):

警告:mysql_num_rows()希望参数1是资源,第58行xxxxxxx中给出的布尔值

根据上面写的内容,我尝试了以下方法,但同样无效:

SELECT * 
FROM 
    (
        SELECT
                (
                    SELECT
                        messageID,
                        posterID,
                        messageTime,
                        message
                    FROM
                        chat_messages
                    WHERE
                        group = '0'
                )
        FROM
            chat_messages
        /* Subquery is used to get the most recent 20 by messageTime */
        ORDER BY 
            messageTime DESC
        LIMIT 20
    ) subq
/* Reorder the result of the subquery to put them back into ascending order */
ORDER BY 
    messageTime ASC
如何使查询仅访问组0中的邮件?我只是不明白它怎么不起作用

谢谢你,
Joe

是MySQL的一个应用程序,在其周围放置backticks
`

SELECT * 
FROM 
(
     SELECT messageID,
          posterID,
          messageTime,
          message
     FROM chat_messages
     WHERE `group` = '0'
        /* Subquery is used to get the most recent 20 by messageTime */
     ORDER BY  messageTime DESC
     LIMIT 20
 ) subq
/* Reorder the result of the subquery to put them back into ascending order */
ORDER BY  messageTime ASC

我不明白为什么要使用子查询。第一个选择与第二个选择类似,只是它颠倒了顺序。 当使用and ORDER子句查询MySQL时,它知道如何正确地对它们进行排序。 给定如下表(聊天)结构: messageID | posterID | messagetTime | message | group |[其他字段], 您可以这样选择它们:


选择messageID、posterId、messageTime、messagefrom chat
其中`group`=[编号]
按消息订购时间描述
限制20

这将为您提供最新的20条消息。
正如@bluefeet所说,GROUP是一个保留字,您应该使用backticks。

谢谢。真不敢相信我错过了!这么简单的错误。呃,我花了大约20分钟想知道为什么它不起作用。我会送出去的我需要检索最后20条消息,然后翻转顺序,以便以所需的方式将它们打印在屏幕上——最新消息位于底部,就在文本区域的上方(就像在智能手机上)。只需以另一种方式遍历响应:P`for(len=response.length(),I=len-1;I>=0;--I){//code}`