MySql join with 2 Tables and 2 join - mysql

I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id and posts.lastuserid = users.id to get username and lastusername.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?

Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers, which is not defined.

Related

SQL LEFT JOIN WHERE

I have two tables, one with settingIDs and values of those settings for each userID and another with userIDs, their emails and user names.
I am trying to join the values of specific settings, the problem is that not all users have this specific setting tied to their ID, so I end up with less rows than I actually need.
Table 1
userID settingID settingValue
Table 2
userID userDOB userEmail userName
My query looks like this:
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
WHERE s.settingID = 1
What do I need to do to get all of the users in the list?
Your where clause turns you left join into an inner join. Put the condition in the JOIN
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
AND s.settingID = 1
you can use UNION ALL clause to combine two JOINS
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT userSettings s
ON u.userID = s.userID
UNION ALL
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
RIGHT userSettings s
ON u.userID = s.userID

SQL join count(*) of one table to another table as aliases

I have two table, users and comments.
In the users table, there're columns id and username.
In the comments table I have user_id and his message.
And I wanted to create a table that select the username and his comment count when I search a particular username.
How do I write this?
my testing attempt:
SELECT COUNT(*) AS comment_count
FROM song_comments
RIGHT JOIN users
WHERE user_id = 7 AND comments.user_id = users.id
Try this:
SELECT U.Username, COUNT(SC.message) AS comment_count
FROM song_comments SC JOIN
users U ON U.id=SC.user_id
WHERE U.user_id = 7
GROUP BY U.Username
This gives you users and count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
group by u.username
You can add a where to get one user's count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
where u.username = 'Hogan'
group by u.username

mysql left join exists

Tables:
users: id, name
usersFacebookFriends: id, uid, friendUid, name
I need a query to search for friends (name), and check if they exists in the users table, so then they are also on the website registered.
Is this possible with a left join, and if so how?
Thanks.
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name;
If you want only those that exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
INNER JOIN users u
ON ff.name = u.name;
If you want only those that don't exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name
WHERE u.id IS NULL;
I guess this should make a job. If facebook friend is not registered with your site corrsponding joined User table fields will be NULL.
SELECT f.name, u.* FROM usersFacebookFriends AS f
LEFT JOIN users AS u
ON f.friendUid = u.id
WHERE f.uid = 123
Change LEFT JOIN to INNER JOIN if you need to find only those who exist.

Need help with a mysql query

I have a payments table that has the following structure.
**Payments**
id
name
created_by - user_id
closed_by - user_id
**Users**
user_id
name
surname
What is the best way to show both the name and surname of the user who has created and closed the payment file.
The only way i can think this could work would be using a subquery for both(created_by,closed_by fields) thanks
Try this:
SELECT p.id, p.name,
CONCAT (u1.name,' ', u1.surname) created,
CONCAT (u2.name,' ', u2.surname) closed,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
EDITED: if you want name and surname splitted
SELECT p.id, p.name,
u1.name created_name, u1.surname created_surname,
u2.name closed_name, u2.surname closed_surname,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
A strict join is not enough, I think you can use joins in the where clause
select name, surname
from
payments p, users u
where
u.user_id = p.created_by
and u.user_id = p.closed_by
You can join to a single table multiple times, but you have to use an alias.
select
p.*,cr.*,cl.*
from
payments p
join users cr
on p.created_by = cr.user_id
join users cl
on p.closed_by = cl.user_id

mysql query: display all entries from guestbook_comments for a specific owner_id

I need some help with query from multiple tables.
My database:
I have following tables:
1. users (user_id, user_name, ..) //user_name is unique.
2. guestbook_comments(owner_id, comment_author_id, comment ..)
3. profile_photo(profile_photo_id, user_id, path)
My problem:
I want to build a following query:
I have url like guestbook.php?user=sudeep. So i get user_name by $_GET['user']; user_name is unique (in the users table).
I want to display all entries from guestbook_comments for a specific owner_id (but i only know user_name, so i will need to find user_id from users table).
For each of the comment_author_id in guestbook_comments table, I want to get the his user_name from table users and path from profile_photo table.
My approach:
First I join users and guestbook_comments table to get all guestbook comments for a specific user_id. Then In the while loop I join users and profile_photo table to get user_name and photo path respectively.
I highly doubt if my approach is any efficient. Can you tell me if there is a right way to do that?
$sql = "SELECT u.user_id, g.owner_id, g.comment_author_id
FROM guestbook g
INNER JOIN users u ON u.user_id = g.owner_id
WHERE u.user_name = $user_name";
while($row = mysql_fetch_array( $result )) {
$author_id = $row['comment_author_id'];
$sql = "SELECT u.user_name, p.path
FROM profile_photo p
INNER JOIN users u ON u.user_id = p.user_id
WHERE u.user_id = $author_id;
//display comment text from guestbook, user_name from users, and photo from profile_photo
}
Am I missing something, or could you combine both joins at once and get all the data with 1 call to the database, rather than 2 calls per user?
$sql = "SELECT u.user_id, g.owner_id, g.comment_author_id, p.path
FROM guestbook g
INNER JOIN users u ON u.user_id = g.owner_id
INNER JOIN profile_photo p on u.user_id = p.user_id
WHERE u.user_name = $user_name";
Try a single query:
SELECT u.user_id, g.owner_id, g.comment_author_id, g.comment_text, c.user_name, p.path
FROM users u
LEFT JOIN guestbook g
ON u.user_id = g.owner_id
LEFT JOIN users c
ON c.user_id = g.comment_author_id
LEFT JOIN profile_photo p
ON p.user_id = g.comment_author_id
WHERE u.user_name = $user_name
From user, find guestbook entries, find commenters, find commenters' photos