Answer the question
In order to leave comments, you need to log in
Using DISTINCT in MySQL only on the right column?
Good afternoon.
I ask for help explaining how to correctly query the MySQL database with the DISTINCT parameter.
The fact is that I don’t need all the columns from the table, only those specified by SELECT "th. At the same time, DISTINCT does not work correctly.
SQL:
SELECT ` user `, ` message `, ` date `, ` read `, ` attach `, ` name `
FROM user_dialog d
JOIN user u
ON u.id = d.why
WHERE (d.user = 1)
Actually the question is: how can I change the query so that the result would be with a unique user_dialog.user field?
Answer the question
In order to leave comments, you need to log in
You need, as I understand it, to group and not to use DISTINCT:
SELECT `user`,`message`,`date`,`read`,`attach`,`name`
FROM user_dialog d
JOIN user u
ON u.id = d.why
WHERE (d.user = 1)
GROUP BY `user`
If we represent the dialog table in the form: dialog(sender_id, recipient_id, message, date ...), then the query will be something like this:
This query will display all unique pairs of interlocutors.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question