How can I query a table and pull out same column info from foreign keys that reference the same table? "SELECT name FROM users INNER JOIN gifts ON to=id WHERE id=1;" will get me the first part but I am unsure on how to get the second part.
You can join the users table twice:
select
u1.name `from`,
u2.name `to`
from gifts g
join users u1 on g.`to` = u1.id
join users u2 on g.`from` = u2.id
where u1.id = 1;
Related
My title isn't great, but let me explain my situation. I have a jobs table. The jobs table has 2 foreign keys to the users table: sales_rep_id and account_manager_id.
Then I have another table called contact_info with a one to one relationship to the users table.
jobs
-----
sales_rep_id
account_manager_id
...
users
-----
first_name
last_name
contact_info
-----
user_id
home_phone
If I want to do a query where I get the phone number for both people on every job I would do the following:
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home FROM jobs
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) reps
ON reps.id = jobs.sales_rep_id
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) account_managers
ON account_managers.id = jobs.account_manager_id
Is there anything I can do to create a temporary table with the joined data? What is the most efficient way to do this join? For example, what if I had 10 foreign keys in the jobs table to the users table, and I needed the phone_number for all 10?
This should be obtain all the info you need
SELECT j.*
, u1.first_name as sales_rep_first_name
, u1.last_name as sales_reps_last_name
, u2.first_name as manager_first_name
, u2.u1.last_name as manager_last_name
, c1.home_phone as sales_rep_home_phone
, c2.home_phone as manager_home_phone
FROM jobs as j
INNER JOIN contact_info as c1 ON j.sales_rep_id = c1.user_id
INNER JOIN user u1 ON u1.id= c1.user_id
INNER JOIN contact_info as c2 ON j.saccount_manager_id = c2.user_id
INNER JOIN user u2 ON u2.id= c2.user_id;
Define a view that joins user and contact_info.
CREATE VIEW user_contact_info
SELECT u.id, u.first_name, u.last_name, c.home_phone
FROM user AS u
JOIN contact_info AS c ON u.id = c.user_id
Then you can use this as if it's a table.
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home
FROM jobs
JOIN user_contact_info AS reps ON reps.id = jobs.sales_rep_id
JOIN user_contact_info AS account_managers ON account_managers.id = jobs.account_manager_id
I've this table structure:
-request
request_id
user_id
-user
user_id
company_id
-company
company_id
I want to select all those records from requests table where user_id=? and no such records where the company id of to users is same.
This is usually achieved using LEFT JOIN:
SELECT r.*
FROM request r
JOIN user u ON r.user_id = u.user_id
LEFT JOIN u1 ON u1.user_id != u.user_id AND u1.company_id = u.company_id
LEFT JOIN request r1 ON r1.user_id = u1.user_id
WHERE r1.user_id IS NULL
By "where" we say that we don't want "users with same company, who has at least 1 request"
I have two MySQL tables, tbl_users and tbl_pagecontent. Inside tbl_pagecontent I have two columns 'UserCreated' and 'UserModified' which contain IDs of users inside tbl_users, and in tbl_users I have 'ID', 'Username',etc.
I guess my question is, how can I query a row, so after the page details (title, content, etc.), I also get the username that created and the username that modified (using corresponding IDs). I tried using a Left Join, but that only works for only one link.
add two left joins to tbl_users, one from UserCreated & one from UserModified
select u1.Username as CreatedBy, u2.Username as ModifiedBy
from tbl_pagecontent
left join tbl_users u1 on u1.id = tbl_pagecontent.UserCreated
left join tbl_users u2 on u2.id = tbl_pagecontent.UserModified
You can assign two different alias to the same table.
SELECT *
FROM tbl_pagecontent p, tbl_users u1, tbl_users u2
WHERE p.usercreated = u1.userid
AND p.usermodified = u2.userid
(OR)
SELECT *
FROM tbl_pagecontent p
LEFT JOIN tbl_users u1 ON p.usercreated = u1.userid
LEFT JOIN tbl_users u2 ON p.usermodified = u2.userid
Suppose I have two table USER and MESSAGE
USER - id,name
MESSAGES - id,message_from,message_to,message_message
So inorder to join I use this co
SELECT m.*,u1.*,u2.* FROM MESSAGES m
INNER JOIN USER as u1
ON(m.message_from = u1.id)
INNER JOIN USER as u2
ON(m.message_to = u2.id)
So now when i print_r out the result in codeigniter it doesn't have the user data for the user message_to.
And I guess even if it return data for both user then it should have some prefixes to differentiate
So how should I do that ?
Thanks and Regards
It is best to avoid using SELECT * syntax.
I would suggest you specify the columns you want back and give those columns whose names are not unique an alias.
SELECT m.id AS MessageId,m.message_from,m.message_to,m.message_message,
u1.id AS FromId, u1.name AS FromName,
u2.id AS ToId,u2.name AS ToName FROM MESSAGES m
INNER JOIN USER as u1 ON(m.message_from = u1.id)
INNER JOIN USER as u2 ON(m.message_to = u2.id)
Use aliases :
SELECT m.id,
u1.id as u1id,
u1.name as u1name,
u2.id as u2id,
u2.name as u2name
FROM MESSAGES m
INNER JOIN USER as u1
ON(m.message_from = u1.id)
INNER JOIN USER as u2
ON(m.message_to = u2.id)
I have 2 tables :
users : contains id and name
friends : contains id1 and id2
Let's suppose I have this data in the users table :
id : 1 , name : FinalDestiny
id : 2 , name : George
And this data in the friends table:
id1: 1 , id2: 2
So this means that 1 is friend with 2.
I need with one single query to say that id1 is friend with id2( but I need the names) Like: FinalDestiny is friend with George. I have to make one mysql inner or left join but I don't know how
Select F1.Name,
F2.Name
From Friends F
INNER JOIN USER F1 ON F.ID1 = F1.id
INNER JOIN USER F2 ON F.ID2 = F2.id
Explanation:
As friend is composition of two user, friend1 and friend2, so we will try to join friend with 2 user, 1 user table corresponding to ID1 and other to ID2 getting information for both the friends from respective user table.
use aliases and double-join the user table with the friends table:
SELECT `u1`.`name` `name1`, `u2`.`name` `name2`
FROM `friends` `f`
INNER JOIN `users` `u1`
ON `f`.`id1` = `u1`.`id`
INNER JOIN `users` `u2`
ON `f`.`id2` = `u2`.`id`
I don't know whether you can do it. But this is the closest that I could get it for you.
select u1.name, u2.name from users u1, users u2, friends f where u1.id = f.id1 and u2.id = f.id2
You'll need to join on your users table twice.
SELECT
u1.name AS FirstPerson
,u2.name AS SecondPerson
FROM
friends f
INNER JOIN
users u1
ON
u1.id = f.id1
INNER JOIN
users u2
ON
f.id2 = u2.id
You need to select from friends and join the users table twice (for each friend)
SELECT u1.Name, u2.Name
FROM Friends as fr
INNER JOIN users as u1 on fr.Id1 = u1.id
INNER JOIN users as u2 on fr.Id2 = u2.id
You should be able to query this without the explicit use of joins using something along the lines of...
SELECT * FROM (friends, users AS u1, users AS u2) WHERE
friends.id1=u1.id AND friends.id2=u2.id