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
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;
I want to do a INNER JOIN on a table on himself to get only one value, so i make
SELECT *,u2.name AS ownername FROM user u INNER JOIN user u2 ON u.owner = u2.id
The problem i have is that if I try for example to print the id value, it takes it from the u2 values... And there is too many fields to rename all of them with u.id AS... u.surname AS...
I think this will be easy to fix but i dont know how :(
Thank you for any idea
You want:
SELECT u.*,u2.name AS ownername FROM user u INNER JOIN user u2 ON u.owner = u2.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 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