richard
richard

Reputation: 1536

MySQL GROUP BY multiple columns and fields

I've tried to search answer here, there are few questions are quite similar with my question but i still can't figure out how to GROUP BY below.

MYSQL Structure:

ID | USERID | FRIENDID 
1  |   14   |    65
2  |   65   |    14
3  |   12   |    19
4  |   19   |    12

How to GROUP BY ID 1 & ID 2 and ID 3 & ID 4 with php query?

Result:

1. 14 & 65 
2. 12 & 19

Upvotes: 3

Views: 216

Answers (2)

John Woo
John Woo

Reputation: 263723

SELECT LEAST(USERID, FRIENDID) as x, 
       GREATEST(USERID, FRIENDID) as y
FROM   TableName
GROUP  BY x, y

Upvotes: 8

Mathew
Mathew

Reputation: 8279

SELECT CONCAT(USERID, ' & ', FRIENDID) FROM table 

Upvotes: -2

Related Questions