Skip to content
Advertisement

MySQL how to solve One-to-one relation

I have a One-to-One Chat implementation with MySQL. The goal is to got for one user all unique Conversations with the last message. Even I’m using MAX on the primary key and group by 2nd column MySQL reject this.

The below query give the error: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘m.id’ which is not functionally dependent on columns in GROUP BY clause;

I’m using MySQL Version 8.

What I’m missing here?

Message table

Conversation table

Query:

Advertisement

Answer

Without sample data, your question is a bit hard to follow. However, you should be using window functions if you are using MySQL 8+. The query would look something like this:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement