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
Related
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;
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
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 two table.
1st table:
Table Name: User
-id
-name
2nd table:
Table Name: Record
-player1ID
-player2ID
-player3ID
-location
-time
I would like to display all records.
I use "SELECT * FROM Record"
However, I would like to display not the player ID, I want to display the name of the player. How can I write a SQL that can do so? Thanks.
You need an INNER JOIN.
The trick is to join the User table multiple times (under different aliases) because you need multiple values from it.
select u1.name as player1, u2.name as player2, u3.name as player3, r.location, r.time
from Record r
inner join User u1 on r.player1ID = u1.id
inner join User u2 on r.player2ID = u2.id
inner join User u3 on r.player3ID = u3.id
It appears you wish to have the 'record' table with names substituted in for the IDs?
In that case you can JOIN the User table to the Record table (read up about joins here):
SELECT u.name as player1, u2.name as player2, u3.name as player3,
r.location, r.time
FROM Record r
JOIN User u ON u.id=player1ID
JOIN User u2 ON u2.id=player2ID
JOIN User u3 ON u3.id=player3ID
select a.name, b.name, c.name, d.location, d.time from Record d, User a, User b, User c where d.player1Id=a.id && d.player2ID = b.id && d.player3ID = c.id;
I am working the friend table with mysql. I want to get friend userid and username form current username.
friend
=======
- id
- uid
- fid
Sample Data
===========
id uid fid
1 1 2
2 3 1
user
====
- id
- username
Sample Data
===========
id username
1 saturngod
2 snow
3 John
My current code is
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.uid
WHERE friend.fid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
UNION
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.fid
WHERE friend.uid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
It's working. I got the friend list. However, I feel , the sql is so long.
Can I reduce this sql and can we write without UNION in sql ?
You have already joined the tables, so the subselect is not needed.
SELECT u.id,u.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
WHERE u.username='saturngod'
UNION ALL
SELECT u2.id,u2.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
INNER JOIN user u2 ON (u2.id = f.fid)
WHERE u.username='saturngod'
Or you can do:
SELECT
u.id as user_id
,u.username
,u2.id as friend_id
,u2.username as friendname
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username
If you want one row per user you can do:
SELECT
u.id as user_id
,u.username
,GROUP_CONCAT(u2.id) AS friend_ids
,GROUP_CONCAT(u2.username) as friendnames
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username <<-- optional
GROUP BY u.id
SELECT CASE WHEN user_id="$id" THEN friend_id ELSE user_id END AS friendID
FROM user_friend WHERE user_id="$id" OR friend_id="$id" order by friendID ASC