Join two tables and display the details - mysql

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;

Related

What would the best query be for selecting values from two separate database tables?

I'm trying to output something based on these database values, there are two tables. users and transactions. users has the following fields:
id, username, firstname, lastname, usertype, points
The second table, transactions has the following:
id, sender_id, receiver_id, points
The sender_ID is the ID of the person who sent the points, receiver_id is the ID of the person receiving, and so on.
How can I query it to output values from both tables in php, so it would display like this:
John Doe has sent Jane Doe 3 points.
Can you try it?
In MYSQL
SELECT CONCAT(u1.firstname,' ',u1.lastname) as sender,CONCAT(u2.firstname,' ',u2.lastname) as receiver,t.points as points
FROM transactions t
INNER JOIN users u1 ON u1.id=t.sender_id
INNER JOIN users u2 ON u2.id=t.receiver_id
In PHP
You retrieve
echo $row['sender'].' has sent '.$row['receiver'].$row['points'].' points';
Just use two joins to the same table:
SELECT u1.firstname, u2.firstname, t.points FROM transactions t
INNER JOIN user u1 ON u1.id = t.sender_id
INNER JOIN user u2 ON u2.id = t.receiver_id
WHERE t.id = 1
Use Joins...
SELECT `t1`.`username`, `t2`.`username`, `t`.`points` FROM transactions `t`
INNER JOIN `table` `t1` ON `t1`.`id` = `t`.`sender_id`
INNER JOIN `table` `t2` ON `t2`.`id` = `t`.`receiver_id`
WHERE `t`.`id` = 1
Hello you can make a LEFT JOIN (eg: http://www.tutorialspoint.com/mysql/mysql-using-joins.htm) or you can use this:
SELECT a.username, a.firstname, a.lasname, b.points, b.receiver_id
FROM table1 a, table2 b
WHERE table1.id = table2.sender_id;

MySQL - How can I get Join data twice from one table

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

Join two table twice and return duplicate column name

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)

Mysql database selecting with relation

I have this table that I hold userlist for my msn application. There's another table for the friendship where it has two foreign keys from userlist.
user: id, name, online, ip...
friend: id1, id2
I want the information of the users that are friend with a specific id.
I'm using this sql query:
SELECT (latest_ip, email, online, pass, status)
from im.user JOIN im.friend ON user.id = friend.id1
WHERE user.id = 5
what am i missing?
remove the parenthesis on your select clause, you don't need them
SELECT latest_ip, email, online, pass, status
from im.user
INNER JOIN im.friend
ON user.id = friend.id1
WHERE user.id = 5
UPDATE 1
you need to have extra join with the table user again since you want to get the informations of the user's friends.
SELECT c.*
from im.user a
INNER JOIN im.friend b
ON a.id = b.id1
INNER JOIN im.user c
ON b.id2 = c.id
WHERE a.id = 5

MySQL inner join single query

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