How can I group this sql query?

How can I group this sql query? I want to print the records only 1 time …

SELECT
friend_id,
friend_adding_id,
friend_added_id,
user_id,
user_name
FROM friends
LEFT JOIN users ON (friends.friend_adding_id = users.user_id OR friends.friend_added_id = users.user_id)
WHERE
friend_adding_id = 1 OR friend_added_id = 1
AND friend_confirm = 1
ORDER BY friends.friend_id DESC
LIMIT 1, 10

Any number of ways, the question is what you’re trying to get out of each group, and what the defining character of the group is.

Return 2 of each query. I just want to each.

i did not understand this

but i looked at your query and noticed the OR in the join’s ON clause

this will perform poorly because it cannot be indexed

the routine solution is to break the query into two queries, one for each of the OR’d conditions, and then UNION these queries together

this will probably simplify your WHERE conditions too

1 Like

I wonder if this is any help?

https://www.w3schools.com/sql/sql_distinct.asp

The SQL GROUPBY clause is used in collaboration with the SELECT statement to arrange identical data into groups . This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.

i’m not sure blindly plagiarizing Tutorialspoint is going to help anyone…

1 Like