1

I have a "chat" table that logs chats between 2 people. Let's call the first person "speaker", and the second person "receiver". When the first person sends a chat message, he will be the "speaker". The second person who receives the message will be the "receiver". When the second person responds, he will be the "speaker", and the first person who receives the response will be the "receiver".

This query works on XAMPP (where ONLY_FULL_GROUP_BY is probably disabled):

SELECT sq.* FROM ( SELECT speaker, receiver, message, status, date_sent FROM chat WHERE speaker = 10001 UNION SELECT receiver, speaker, message, status, date_sent FROM chat WHERE receiver = 10001 ORDER BY date_sent DESC ) AS sq GROUP BY sq.speaker, sq.receiver ORDER BY date_sent DESC 

If I have this sample data (produced when I run a simple "SELECT * FROM chat" query):

speaker | receiver | message | status | date_sent<br> 10001 | 10001 | how are you? | read | 2018-01-30 07:30:15<br> 10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br> 10001 | 10003 | hello dear | read | 2018-01-30 07:35:29<br> 10003 | 10001 | hello too! | read | 2018-01-30 07:36:01<br> 10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br> 10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br> 

I get this sample output (my desired output):

speaker | receiver | message | status | date_sent<br> 10002 | 10001 | fine, thanks | unread | 2018-01-30 07:32:43<br> 10001 | 10003 | how are you? | unread | 2018-01-30 07:37:24<br> 10001 | 10004 | hey! | read | 2018-01-30 07:36:38<br> 

But when I run the query on my Ubuntu PC (where ONLY_FULL_GROUP_BY is probably enabled), I get this error:

Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sq.message' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

What is an alternative query that I can use to produce my desired output?

1
  • I may have found an answer. I think I can do something like this in the outer query: SELECT MAX(id), speaker, receiver, GROUP_CONCAT(message), GROUP_CONCAT(date_sent) FROM... Got the answer here: stackoverflow.com/questions/47850148/… -- answered by Bill Karwin Commented Jan 31, 2018 at 3:07

1 Answer 1

1

I may have found an answer. I think I can do something like this in the outer query:

SELECT speaker, receiver, GROUP_CONCAT(message), GROUP_CONCAT(date_sent) FROM chat... 

Got the answer here: How to fix query group with only_full_group_by… -- answered by Bill Karwin. Thanks Frank AK for trying.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.