select f1 from (
select friend2 as f1 from friends where friend1 = 1
UNION
select friend1 as f1 from friends where friend2 = 1
) as s1
select if(friend1 = 1, friend2, friend1) as f1
from friends
where (friend1 = 1 or friend2 = 1)
select * from users where id in (
select f1 from
(select if(friend1 = 1, friend2, friend1) as f1 from friends where (friend1 = 1 or friend2 = 1)) as s1
INNER JOIN
(select if(friend1 = 49519, friend2, friend1) as f2 from friends where (friend1 = 49519 or friend2 = 49519)) as s2
on s1.f1 = s2.f2
)
select * from users where id in (
select f1 from (
select friends.friend1 as f1 from friends where friends.friend2 = 1
UNION ALL
select friends.friend2 from friends where friends.friend1 = 1) as s1
INNER JOIN (
select friends.friend1 as f2 from friends where friends.friend2 = 49519
UNION ALL
select friends.friend2 from friends where friends.friend1 = 49519
) as s2 ON s1.f1 = s2.f2)
SELECT user_id, MAX(sum1), some_id FROM (
SELECT user_id, COUNT(some_id) AS sum1, some_id FROM table1 GROUP BY some_id, user_id ORDER BY sum1 DESC) as t1
GROUP BY user_id
SELECT DISTINCT (JSON_EXTRACT(data, '$.name')) FROM table
но не знаю пока как быть с тем, что у вас это массивы. с ними сложнее, нужно будет кастить видимо