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