SQL: Join on multiple tables - mysql

I am trying to join the following tables:
Table 1: users (id, name)
Table 2: pivot_table (user_id, user_id2)
This table hold the relationship between the users. One user can be related to many other users.
Table 3: contacts (id, user_id, name)
I am trying to select a user and then find its related users and contacts.
I have tried to use the join, i am successfully able to get either the contacts or the users but not the both.
I used the following query:
select
contacts.id as contact_id,
users.id as user_id,
pivot.user_id2 as user2_id
from
`contacts`
inner join `users` on `contacts`.`user_id` = `users`.`id`
inner join `pivot` as `pivot` on `pivot`.`user_id` = `contacts`.`user_id`
Suppose i have the following data in the tables
Users:
id name
1 Sahil
2 Shubham
3 Xyz
Pivot table data
user_id user_id2
1 2
Contacts table data
id user_id name
1 1 Abc
2 1 XYZ
Now what is get from the query is
contact_id user_id user_id2
1 1 2
2 1 2
but what i need is
contact_id user_id user_id2
1 1 null
2 1 null
null 1 2
I am not able to figure out what wrong i am doing.
Thanks

Try this instead:
SELECT B.id as contact_id, A.id as user_id, C.user_id2 as
user2_id
FROM `users` A LEFT JOIN `contacts` B
ON A.`id`=B.`user_id` LEFT JOIN `pivot` C
ON A.`id`=C.`user_id`;
See MySQL Join Made Easy for insight on using joins.

SELECT users.id,GROUP_CONCAT(contacts.user_id,pivot.user_id SEPERATOR ",")contacts.user_id,pivot.user_id
FROM contacts INNER JOIN users ON contacts.user_id=users.id INNER JOIN pivot ON users.id=pivot.user_id;

I solved the issue by myself with the use of Union to get the desired results.
Thanks for your support.

It seems you want a query to show unrelated data:
a user's contacts
a user's related users
I'd suggest two separate queries for this. If you want to do this in one query, use UNION ALLto combine two query results:
select contact_id, user_id, null as user_id2 from contacts
union all
select null as contact_id, user_id, user_id2 from pivot_table;

Related

I'm trying to match different tables without a primary key

before posting this, I searched for an answer, but couldn't find the exact solution to my issue.
First of all, I am only assuming that I need INNER JOIN to solve it. FULL OUTER JOIN might also do the trick.
I have 3 different tables in one database.
TABLE 1
userId
roleID
1
1
2
2
TABLE 2
userId
userName
1
A
2
B
TABLE 3
roleId
roleName
1
X
2
Y
My goal is to write a query, which matches the userName from Table 2 with the roleName of Table 3 in the output. It should look like this:
Output
UserName
RoleName
A
X
B
Y
I can't figure it out. Any help would be appreciated.
Thank you very much!
EDIT:
so far I am trying to modify the following query to do the job:
SELECT * FROM
table1.roleId
INNER JOIN
table2.roleId
ON table1.roleId = table3.roleId
INNER JOIN
table2.userId
ON table2.userId = table3.roleId
Just join all tables together with following conditions:
Table 2 userId = Table 1 userId
Table 3 roleId = Table 1 roleId
Then select only those values which are relevant to you.
I think this should do the job:
select t2.userName, t3.roleName
from table2 t2
join table1 t1 on t2.userId = t1.userId
join table3 t3 on t3.roleId = t1.roleId;
The query above uses INNER JOINs to get the result.
If there are userIds without a roleId in Table 1, FULL OUTER JOIN would also return userNames with empty roleNames if such entries are present.

How do I Select from two tables using Two WHERE Conditions?

I have an application that has a referral system where users can use their user_id to refer another user. All users are on the same table. The referrals table stores the user_id for the referrer and for the referred.
Users Table:
Referrals Table:
I want to query the names of both users (referrer and referred) from the user's table using their user_id that is populated in the referrals table.
Expected Result:
select t2.name as referrer_name,t3.name as referred_name
from REFERRALS_TABLE t1
left join USER_TABLE t2 ON t1.referrer=t2.user_id
left join USER_TABLE t3 ON t1.referred=t3.user_id
I method is to join the User_table twice -
SELECT U1.name referrers_name, U2.name referred_name
FROM Referrals R
JOIN Users U1 ON R.referrer = U1.user_id
JOIN Users U2 ON R.referrd = U1.user_id
You use sql UNION if get two table data and multiple where clouse.
SELECT * FROM oauth_clients WHERE user_id=2
UNION
SELECT * FROM photos where gallery_id=12

Get user id via mysql

I have the two tables grades and feedback and the following MySQL query:
SELECT id, mingrade, maxgrade, quizid
FROM feedback
WHERE quizid=6
ORDER BY id DESC LIMIT 6
How do I need to modify the query to add userid at the first table?
What are the fields of the grades and feedback tables? Use a JOIN that links the two based on a common attribute.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
WHERE condition;
So if your two tables are grades and feedback, and both have the field userid you could say:
SELECT id, userid, mingrade, maxgrade, quizid
FROM feedback
INNER JOIN grades ON feedback.userid = grades.userid
WHERE quizid = 6
ORDER BY id DESC LIMIT 6;
I think that should work.

How to get ONLY common rows of 2 tables on MySQL?

Probably is something simple, but, let's say I have these:
Table User (id_user, name)
Table A (id_a, name, type, #id_user)
And then I have another one that have only its own id and the other foreign keys
Table B (id_b, #id_user1, #id_user2, #id_a, #id_Something)
So, I need a query that returns ONLY the rows of table A and table B with what they have in common. I've tried INNER JOIN but it returns all rows of Table A where the id_user from there is equal to the id_user from table B. Like, if I have these:
Table User:
id_user name
1 Hey
Table A:
id_a name type id_user
1 a car 1
2 b cat 1
Table B:
id_b id_user id_user2 id_a id_Something
1 1 Doesn't matter 1 Doesn't matter
I need to return only the common row between Table A and Table B (that'll be something like:
id_a name type id_user id_b id_user2
1 a car 1 1
I've tried INNER JOIN but it returns to me everything when the id_user from A = id_user from B. I used this syntax:
SELECT *
FROM B
INNER JOIN A ON A.id_user = B.id_user;
Hope I've made myself clear, thank you a lot.
Is what you're going after: "Show me all the rows in A and B which share the same id_user"
SELECT User.id_user, User.name, a.id_a, b.id_b
FROM User
INNER JOIN A ON a.id_user = User.id_user
INNER JOIN B on b.id_user = User.id_user

Which entries in my list do not have an entry in my table?

How do I do something like this in MySQL?
(1274649,682844,872502,1016256)
INTERSECT
(SELECT id FROM `users` WHERE `id` IN (1274649,682844,872502,1016256))
Adapting from the comments:
These four numbers are the IDs that I have now. I want to know which of these IDs do not have an an entry in my table, and how many of them don't have an entry?
select t.id from (
select 1274649 as id union
select 682844 union
select 872502 union
select 1016256
) t
left join users u on u.id = t.id
where u.id is null
This returns those ids that haven't corresponding id in users table.
Added this is the answer to OP explanation in comment: Which entries in my list do not have an entry in my table?