我正在开发类似 Tinder 的应用程序,我想获取所有比赛。在我的比赛表中,每个用户都有自己的条目。因此,对于一场比赛,有两个用户,表中总共有两个条目。
我尝试了如下所示的 sql 查询
从匹配中选择 user_id、friend_id,其中 is_match = 1 按 user_id、friend_id 分组
通过查询我得到以下结果 `
|--------------------------|
|user_id|friend_id|is_match|
|--------------------------|
| 23 | 24 | 1 |
|--------------------------|
| 24 | 23 | 1 |
|--------------------------|
| 24 | 25 | 1 |
|--------------------------|
| 25 | 24 | 1 |
|--------------------------|
| 25 | 26 | 1 |
|--------------------------|
| 26 | 25 | 1 |
---------------------------
`
我想要这样的结果。
`
|--------------------------|
|user_id|friend_id|is_match|
|--------------------------|
| 23 | 24 | 1 |
|--------------------------|
| 24 | 25 | 1 |
|--------------------------|
| 25 | 26 | 1 |
|--------------------------|
`
最佳答案
您可以通过对 user_id
和 friend_id
值进行排序,然后仅选择 DISTINCT
对来获得所需的结果。请注意,不需要 GROUP_BY
。
SELECT DISTINCT LEAST(user_id, friend_id) AS user1, GREATEST(user_id, friend_id) AS user2
FROM matches
WHERE is_match = 1
关于mysql - 如何像tinder一样获得匹配,每个用户在表中都有自己的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55932569/