Answer the question
In order to leave comments, you need to log in
How to perform this type of query in MySQL?
There is a table
| id | user_id | a_id | b_id |
SELECT * FROM `table` WHERE `user_id` = 1
| id | user_id | a_id | b_id |
1 1 2 4
5 1 7 5
7 1 3 4
8 1 9 5
| user_id | matches_row_count |
5 1
8 5
9 32
10 7
user_id = 1
user_rows = []
for row in table:
if row.user_id == user_id:
user_rows.append(row)
result = {}
for user_row in user_rows:
for row in table:
if (user_row.a_id == row.a_id) and (user_row.b_id == row.b_id) and (user_row.id != row.user_id):
if row.user_id not in result:
result[row.user_id] = 1
else:
result[row.user_id]++
Answer the question
In order to leave comments, you need to log in
select
row.user_id, count(*) matches_row_count
from users user_row
join users row on
(user_row.a_id = row.a_id) and
(user_row.b_id = row.b_id) and
(user_row.id != row.user_id)
where user_row.user_id = 1
group by row.user_id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question