i've been trying to join to one table but using two different columns. Ok so i've got the following tables:
Users:
--------------------------------------------------
| user_id | name |
--------------------------------------------------
| 1 | Bob |
--------------------------------------------------
| 2 | John |
--------------------------------------------------
Table2:
--------------------------------------------------
| user_one | user_two | field3 |
--------------------------------------------------
| 1 | 2 | something here |
--------------------------------------------------
Is their any way to join the user's table using both fields. Something like this:
SELECT
table2.*,
users.name
FROM table2
INNER JOIN users
ON users.user_id = table2.user_one AND users.user_id = table2.user_two
I've tried the above query but it doesn't return anything.
Edit
I would want to return something like this:
--------------------------------------------------
| user_one_name | user_two_name | field3 |
--------------------------------------------------
| Bob | John | something here|
--------------------------------------------------
You need two joins. This is often done using left join in case one or both user ids do not match:
SELECT t2.*, u1.name as user1name, u2.name as user2name
FROM table2 t2 LEFT JOIN
users u1
ON u1.user_id = t2.user_one LEFT JOIN
users u2
ON u2.user_id = t2.user_two;
It sounds like you want to get the name of both users in a single row. Assuming so, then you can join to the users table multiple times:
SELECT
table2.*,
u1.name user1name,
u2.name user2name
FROM table2 t2
INNER JOIN users u1 ON u1.user_id = t2.user_one
INNER JOIN users u2 ON u2.user_id = t2.user_two
As mentioned in a comment, if it's possible you have user ids in table2 that do not exist in the users table (or you allow null values to be stored), you can use an outer join instead to get your desired results.
However, if that is the case, you may have larger issues with your database design. You shouldn't have foreign keys that don't have corresponding primary keys unless those are perhaps nullable.
Related
i have two tables like this:
TABLE: users
id | username
----------------
1 | nick1
2 | nick2
TABLE: messages
id | from | to | text
--------------------------------
1 | 1 | 2 | Hi man!
2 | 2 | 1 | Oh, hi.
So, i need SELECT code for replacing "from" and "to" with usernames.
I need SELECT code for my app withe replaced ID´s with usernames :).
Many thanks to all.
You need to join the table messages two times with the table users:
SELECT
m.id,
u1.username,
u2.username,
m.text
FROM
messages AS m
INNER JOIN
users AS u1 ON u1.id = m.from
INNER JOIN
users AS u2 ON u2.id = m.to
You have to join users table twice with aliasses:
select * from messages
join users ufrom on from = ufrom.id
join users uto on uto.id=to
Suppose I have a table named users consist of columns: user_id, user_name, user_created_by.
+------------------+----------------------+-------------------+
| user_id + user_name + user_created_by +
+------------------+----------------------+-------------------+
| 1 | John | 1 |
| 2 | Ann | 1 |
| 3 | Paul | 2 |
| 4 | King | 2 |
| 5 | Dirk | 3 |
+------------------+----------------------+-------------------+
The value of user_created_by is the user_id who created that record. Now, I want to make a query that results one specific row with added column let's say user_created_by_name which is the user_name of the user_id from the user_created_by. Suppose we want to get "Paul"'s record with who (the name) create it (temporary new column). For ease of understanding this is my expected result:
+----------+--------------+-------------------+------------------------+
| user_id | user_name | user_created_by | user_created_by_name |
+----------+--------------+-------------------+------------------------+
| 3 | Paul | 2 | Ann |
+----------+--------------+-------------------+------------------------+
this is my query using codeigniter:
$query=$this->db->query("SELECT *,
(SELECT user_name FROM users WHERE user_id = user_created_by)
AS "user_created_by_name" FROM users WHERE user_id=3);
But my result are:
+----------+--------------+-------------------+------------------------+
| user_id | user_name | user_created_by | user_created_by_name |
+----------+--------------+-------------------+------------------------+
| 3 | Paul | 2 | NULL |
+----------+--------------+-------------------+------------------------+
You culd use a self join (join the same table two time) using alias for fere to the tables as different sets of data
SELECT a.user_id, a.user_name, a.user_created_by, b.user_name as user_created_by_name
from users a
inner join user b on a.user_created_by = b.user_id
where a.user_id = 3
use self join
select u1.user_id, u1.name as user_name,
u2.user_created_by
,u2.user_name as createdby from users u1
join users u2 on u1.user_id=u2.user_created_by
where u1.user_id=3
You can solve this problem using a JOIN.
$sql = "SELECT users.user_id, users.user_name, user_created_by_name.user_name,
FROM users JOIN users AS user_created_by_name ON users.user_id = user_created_by_name.user_id WHERE users.user_id = 3";
$query=$this->db->query($sql);
If you you have users that were not created by another user use a LEFT JOIN instead:
$sql = "SELECT users.user_id, users.user_name, user_created_by_name.user_name,
FROM users LEFT JOIN users AS user_created_by_name ON users.user_id = users.user_id WHERE user_created_by_name.user_id = 3";
$query=$this->db->query($sql);
This will work:
SELECT a.user_id as User_id,
a.user_name as Name,
b.user_id as Created_by_user_id,
b.user_name as Created_by_name
FROM users AS a
INNER JOIN users AS b
ON a.user_id = b.user_created_by
WHERE a.user_id = 3
It is called a self-join, which is used when combining two records of the same table.
I have a three tables that are left join. two of them have uid as user id. Problem is every time query execute it takes the same user id.
Please check the table structure
-------------------------------------
|Users |posts |favorites |
|-----------|-----------|-----------|
|user_id |id |id |
|username |title |uid |
|password |post |post_id |
| |uid | |
| |favorites | |
-------------------------------------
MySQL query
SELECT * FROM favorites
LEFT JOIN users ON users.user_id = favorites.uid
LEFT JOIN posts ON favorites.posts_id = posts.id
WHERE favorites.uid='$id' and posts.active=1
please note that $id is the profile owner (user) id. Any help will be appreciated.
If the table was as follows (I've renamed user_id, post, and favorites columns to clarify their roles as I understand them)
-------------------------------------
|Users |posts |favorites |
|-----------|-----------|-----------|
|id |id |id |
|username |title |uid |
|password |post_text |post_id |
| |uid | |
| |fav_id | |
-------------------------------------
This sql code can be used to get what (I think) you want
SELECT * FROM favorites
LEFT JOIN posts ON favorites.id = posts.fav_id
LEFT JOIN users ON posts.uid = users.id
WHERE favorites.uid='$id' and posts.active=1
This should get all the details of favorite posts from the three tables for the given user id. Hope it works for you.
This query may solve your problem.
SELECT * FROM post p
LEFT JOIN (SELECT * FROM users u LEFT JOIN favorites f ON u.user_id = f.id) as tbl1
WHERE p.id=tbl1.post_id and p.uid = $uid and p.active=1
Hey can you please try this once
SELECT posts.*, favorites.post_id, users.username FROM favorites LEFT JOIN users ON favorites.uid = users.user_id LEFT JOIN posts ON favorites.post_id = posts.id
WHERE users.user_id='$id' and posts.active=1
As there was a typo in the query and i have made few changes to your code
users table:
id | win | lose
0 | 1 | 2
1 | 2 | 8
games table:
id | user1 | user2 | data
0 | 0 | 1 | 'some text'
The DB is for a simple multiplayer game. I need a query to get user2, data, and win-lose data for both users. Is this possible in MySQL at all? Any help would be greatly appreciated.
You have to JOIN the table users two times like this:
SELECT
u1.win AS win1,
u1.lose AS lose1,
u2.win AS win2,
u2.lose AS lose2,
...
FROM games g
INNER JOIN users u1 ON g.user1 = u1.id
INNER JOIN users u2 ON g.user2 = u2.id
You can join the users table twice:
SELECT
games.user2 AS user2_id
games.data AS games_data
user1.win AS user1_win,
user1.lose AS user1_lose,
user2.win AS user2_win,
user2.lose AS user2_lose
FROM games
JOIN users AS user1 ON user1.id = games.user1
JOIN users AS user2 ON user2.id = games.user2
WHERE games.id = ...
I am new to this kind of relational type of database design. I just designed the database in this manner. However, I am quite confused on this JOIN of MySQL. What should be my query to join all this table. If you can see the table users is the reference of all the tables.
users
+----------+----------------+-----------------+
| users_id | users_level_id | users_status_id |
+----------+----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
+----------+----------------+-----------------+
users_credentials
+----------+---------------------------+-----------------------------+----------------------------+
| users_id | users_credential_username | users_credential_email | users_credential_password |
+----------+---------------------------+-----------------------------+----------------------------+
| 1 | super | super#gmail.com | $5$e94e9e$vptscyHjm8rdX0j6 |
| 2 | admin | admin#gmail.com | $5$fVuOmySyC0PttbiMn8in0k7 |
+----------+---------------------------+-----------------------------+----------------------------+
users_level
+----------------+-------------------------+
| users_level_id | users_level_description |
+----------------+-------------------------+
| 1 | Super Administrator |
| 2 | Administrator |
+----------------+-------------------------+
users_status
+-----------------+--------------------------+
| users_status_id | users_status_description |
+-----------------+--------------------------+
| 0 | Disabled |
| 1 | Enabled |
+-----------------+--------------------------+
Try this
SELECT u.*, uc.*, ul.*, us.*
FROM users u
INNER JOIN users_credentials uc
ON u.users_id = uc.users_id
INNER JOIN users_level ul
ON u.users_level_id = ul.users_level_id
INNER JOIN users_status us
ON u.users_status_id = us.users_status_id
Note the use INNER JOIN: this means that if a user does not have corresponing record on joined table it won't be shown; if you need to return every user even without matching record on related tables, change INNER JOIN with LEFT JOIN.
EDITED after user comment:
If you want to return just some column, define it as this example
SELECT uc.users_credential_username AS username,
uc.users_credential_email AS email,
uc.users_credential_password AS pwd,
ul.users_level_description AS level,
us.users_status_description AS status
This is a simple query that will join all of them
select *
from users
left join users_credentials
on users_credentials.users_id = users.users_id
left join users_level
on users_level.users_level_id = users.users_level_id
left join users_status
on users_status.users_status_id = users.users_status_id
EDIT
if you want to fetch data from different tables
user this
select users.* , users_credentials.* , users_level.* , users_status.*
from users
left join users_credentials
on users_credentials.users_id = users.users_id
left join users_level
on users_level.users_level_id = users.users_level_id
left join users_status
on users_status.users_status_id = users.users_status_id
I think this look like this :
SELECT * FROM users
LEFT JOIN user_credentials ON users.user_id = user_credential.user_id
LEFT JOIN user_level ON users.users_level_id = users_level.users_level_id
and so on..
Use this type of query....
SELECT c.*, l.*, s.*
FROM users AS u
INNER JOIN users_credentials AS c ON (u.users_id = C.users_id)
INNER JOIN users_level AS l ON (u.users_level_id= l.users_level_id)
INNER JOIN users_status AS s ON (u.users_status_id= s.users_status_id)
Where you can specify the field what you want in .* ...
Join is used to fetch data from normalized tables which have foreign key relation with the reference table.
For the above table with join you can fetch data among two tables with the help of reference table.
For example
Select * from users a JOIN users_credentials b
ON a.user_id=b.user_id JOIN users_level c
ON c.users_level_id=a.users_level_id
where users_credential_username='super';
The result of this query will give you the detail like users_level_description for the user with users_credential_username=super.