Sample Table:
User1 | User2
------+------
123 | 555
123 | 1
123 | 2
456 | 2
555 | 456
12 | 123
12 | 456
Input: I enter the list (123,456) to look at rows containing either of those values. Then I want MySQL to check the opposite/other column in that row, and group the output by that value.
Output:
User | Count(*)
-----+---------
555 | 2
2 | 2
1 | 1
555 count is 2 because row 123, 555 and row 555, 456 both contain one of the inputs: 123 and 456.
I've tried looking at the CASE keyword, because the obvious obstacle here is grabbing the opposite/remaining column and using that as one of the returned values.
Completely wrong, but one of my half-finished approaches.
SELECT user, count(*)
FROM friendships
WHERE User1 IN (123,456) AS user
OR User2 IN (123,456) AS user
the tricky part with this is your criteria is the IN() is looking in the users1 column and then tries to find duplicates in the user2 column... so you need a join with a UNION
SELECT user2, COUNT(*)
FROM
( SELECT user1, user2
FROM friends f
WHERE f.user1 IN(123, 456)
UNION ALL
SELECT f.user1, f.user2
FROM friends f
JOIN friends f1 ON f1.user1 = f.user2
WHERE f.user1 IN(123, 456)
)t
GROUP BY user2;
WORKING FIDDLE
Related
I have fought my way through various answers and did some progress, however, the final solution has not been discovered.
The DB situation:
Table "clients_a":
userid | name
1 | Steve
2 | John
3 | Paul
Table "clients_b":
userid | name
1 | NULL
3 | Jokename
4 | Jessy
Desired result/output:
userid | name
1 | Steve
2 | John
3 | Paul
4 | Jessy
Description of what is going on:
userid is unique in the result (merged)
a result for name that is not NULL is favored
if two entries, then result from table clients_a is favored
all entries have a groupid (see below), that has to be taken into account
MySql queries I tried (and came close):
Attempt 1: This query works, but it does not regard the name. It takes all names from client_a:
SELECT * FROM
(
SELECT userid, name
FROM `client_a`
WHERE groupid = 123
UNION DISTINCT
SELECT userid, name
FROM `client_b`
WHERE groupid = 123
) AS res
GROUP BY res.userid
Attempt 2: This query creates duplicate entries (one userid can occur twice), but regards the name, as it seems:
SELECT o.*, i.* FROM
(
SELECT userid, name
FROM `client_a`
WHERE groupid = 123
UNION DISTINCT
SELECT userid, realname
FROM `client_b`
WHERE groupid = 123
GROUP BY userid
) AS o
LEFT JOIN `client_a` as i on i.userid = o.userid
I also tried to use MIN(name) without success.
Any help is appreciated.
You can do it with NOT EXISTS:
SELECT a.userid, a.name
FROM clients_a a
WHERE a.groupid = 123
AND (name IS NOT NULL OR NOT EXISTS (SELECT 1 FROM clients_b b WHERE b.userid = a.userid))
UNION
SELECT b.userid, b.name
FROM clients_b b
WHERE b.groupid = 123
AND NOT EXISTS (SELECT 1 FROM clients_a a WHERE a.userid = b.userid AND a.name IS NOT NULL)
See the demo.
Results:
userid
name
1
Steve
2
John
3
Paul
4
Jessy
I have 2 tables:
Users:
------------------------------
id | name
--------------------------
1 | John
2 | Dane
3 | Foo
4 | Bar
Matches Table:
----------------------------
id | userid1 | userid2
----------------------------
1 | 1 | 3
2 | 2 | 4
Question:
From the matches table with id 1, i want to fetch John and Foo in one query. How can i do that ?
I already have one solution but its dull. That is selecting records from matchs tables and then while looping, trigger queries for getting names. .
Just use a JOIN...
SELECT u1.*, u2.*
FROM Matches m
JOIN Users u1 ON u1.id = m.userid1
JOIN Users u2 ON u2.id = m.userid2
WHERE m.id = [ YOUR DESIRED USER ID (for example: 1) ]
SELECT name from Matches, Users where Matches.id = 1 AND (Users.id = Matches.userid1 OR Users.id = Matches.userid2)
This should work.
I have these 2 tables: users and friendship. i would like find the friends in common between two user, i tried to do some query with alias but doesn't show my results.
Table users
user_id | name | surname
1 Luca Jhon
2 Paul Red
3 Jin Blue
4 Diana Lars
Table friendship
id_friendship | id_user_sender | id_user_receive | confirm
1 1 2 2
2 2 3 2
3 1 3 2
4 1 5 2
Should be show this one if i am the user called Luca (id 1 ) and search the realtion with Paul (id 2)
name | surname | id_user |
Jin Blue 3
Any idea? Thank you
Friendship is, presumably, reciprocal. Your friendship table only has one-way relationships.
So, the idea is to create all possible friendships in both directions. Then to aggregate by the first and test the second for each of the users you are interested in:
select u.*
from (select id_user_sender as id1, id_user_receive as id2
from frienship f
union all
select id_user_receive as id1, id_user_send as id2
from frienship f
) f join
users u
on f.id1 = u.user_id
group by id1
having max(id2 = 1) > 0 and
max(id2 = 2) > 0;
I work on my tiny messages system on MySQL.
I have the table "con":
id | user1 | user2
1 | 1 | 3
2 | 3 | 5
3 | 2 | 3
4 | 5 | 8
On, example logged user id = 3.
I want get from table user's id which user_id = 3 is talking.
Correct result for user id = 3:
id | user_id
1 | 1
2 | 5
3 | 2
Thanks :)
You can do this in the where and with a condition in the select:
select id,
(case when user1 = 3 then user2 else user1 end) as user
from con
where 3 in (user1, user2);
This only requires one pass over the table.
Select the two groups of results (user1 = 3 and user2 = 3) and merge them together using UNION.
SELECT id, user1 AS user_id
FROM con
WHERE user2 = '3'
UNION
SELECT id, user2 AS user_id
FROM con
WHERE user1 = '3'
SELECT a.id, a.user1 AS user_id FROM con a WHERE user2 = 3
UNION
SELECT b.id, b.user2 AS user_id FROM con b WHERE user1 = 3
I am new to sql, and been given a task to join 3 tables. Could someone please shed some light on this sql.
I have 3 tables, like
Table 1
userID | username | password
1 user1 user1
2 user2 user2
table 2
userID | order number | order quantity
2 101 100
table 3
userID | name | address
1 John xxx
2 Will xxx
3 Peter xxx
And the result table needs to be displayed as:
UserID | username | name | address | order number | order quantity
1 user1 John xxxxx 0 0
2 user2 Will xxxxx 101 100
3 user3 Peter xxxxx 0 0
You should use LEFT JOIN with COALESCE on some columns,
SELECT a.`userid`,
a.`username`,
c.`name`,
c.`address`,
COALESCE(b.`order number`, 0) `order Number`,
COALESCE(b.`order quantity`, 0) `order quantity`
FROM table1 a
LEFT JOIN table2 b
on a.userid = b.userid
LEFT JOIN table3 c
on a.userid = c.userid
Basically, what LEFT JOIN does is to retrieve all rows from the left table whether it has a match on the second table or not. COALESCE handles what null values should be looked like.
This will help
select
a.userID,
c.name,
c.address,
b.order_number,
b.order_quantity
from
table1 a
join table3 c
on a.userID=c.userID
join table2 b
on a.userID=c.userID
However, you might need to pick up a SQL book as this is a very simple thing to do.