0

I am using the following MySQL query to access the latest 20 messages in a chat log and reverse the order so that the latest message is printed on screen last:

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

It works well. The problem is that I am now trying to add groups to the chat functionality. In doing this I have added a further column to the table chat_messages called 'group'. The main chat log is group 0 so I need to change the above query to access only messages from the main chat log. This is where I am stuck. It appears that MySQL will not allow me to add a where clause within the sub-query. I have tried the following and it did not work:

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

I get this error message (line 58 is the next line after the query):

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in xxxxxxx on line 58

Following what was written on this thread I tried the following, which also did not work:

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

How can I make the query only access messages from group 0? I just don't understand how it is not working?

Thank you,
Joe

4

2 回答 2

1

GROUP is a reserved word for MySQL, place backticks ` around it.

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
于 2012-08-23T12:00:50.280 回答
0

I don't see why you use a subquery. The first select is like the second one, except it reverses the order. When querying MySQL with and ORDER clause, it knows to order them properly. Given a table (chat) structure like this: messageID | posterID | messagetTime | message | group | [other fields], you can select them like this:

SELECT messageID, posterId, messageTime, message FROM chat WHERE `group` = [number] ORDER BY messageTIME DESC LIMIT 20

This will give you the latest 20 messages. And as @bluefeet said, GROUP is a reserved word and you should use backticks.

于 2012-08-23T12:06:40.153 回答