How to LEFT JOIN three tables with the same column name - mysql

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

Related

MySQL SELECT from two tables, replace ID's with usernames

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

MySQL JOIN 3 tables search by WHERE on ID from 1

I seem to be struggling grasping the concept of joining 3 tables on one ID.
USER Table
|id |
|:----:|
|1 |
|2 |
POST table
|id |userid | content|
|:----:|:----:|:----:|
|1 |1 |HELLO |
|2 |1 |THERE |
|3 |2 |WORLD |
friend table
|id | userid | friendid |
|:----:|:------:|:--------:|
| 1 | 1 | 2 |
I want to provide the ID from the USER table, and get ALL the CONTENT from POSTS where the POST.USERID is the FRIEND.USERID and FRIEND.FRIENDID
So USER.ID = '1' would bring back all the POST.CONTENT(in this case).
Really hope you can help, been annoying me for hours.
Following your description, you would not need to join to the user table, you could directly use the friends table, as you have the userid there. But this should give you all posts from userid 5 and his that persons friends.
SELECT post.*
FROM post
INNER JOIN friends ON post.userid = friends.userid OR post.userid = friends.friendid
WHERE friends.userid =1
To not query duplicates use this:
SELECT post.*, users.firstname
FROM post
INNER JOIN users ON users.userid = post.userid
WHERE post.userid = 1
UNION
SELECT post.*, users.firstname
FROM friends
INNER JOIN post ON friends.friendid = post.userid
INNER JOIN users ON users.userid = friends.friendid
WHERE friends.userid = 1

Join Tables On Two Columns

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.

How to restrict join clause via where clause

I have two tables, comment and user. Here is my structure:
// comment
+----+---------+---------+---------+
| id | id_user | id_post | content |
+----+---------+---------+---------+
// user
+----+------+
| id | name |
+----+------+
I want to access the name of user from user table (instead of id_user). here is my query:
select c.content, u.name from comment c inner join user u on c.id_user=u.id;
It gives me this structure:
+---------+------+
| content | name |
+---------+------+
The structure is fine, but I need to just select the comments that are belong to post x. In other word, how can I use id_post in my query?
You can do this with where:
select c.content, u.name
from comment c inner join user u on c.id_user = u.id
where c.id_post = <x>
How about using the WHERE clause:
SELECT c.content, u.name
FROM comment c INNER JOIN user u
ON c.id_user=u.id
WHERE c.id_post = x

joining 4 tables

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.