Combine two tables and change user id to user name - mysql

I have a problem (first problem is that i have a hard time understand how SQL joins work ;)
But i have two tables, one with user_id´s and user_names, and than i have a table with "connection", users can follow each other.
And i want to change the query so i get usernames from that second table, with connection.
Here is a SQLfiddle http://sqlfiddle.com/#!9/1779d/4
And I'm been trying with something like this:
SELECT users_followers.user_id,
usernames.username
FROM users_followers
JOIN users usernames ON users_followers.follower_id = usernames.id
But with no luck.
so SELECT user_id, follower_id from users_followers WHERE follower_id = 1
gives me
+---------+-------------+
| user_id | follower_id |
+---------+-------------+
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
+---------+-------------+
But i want
+---------+-------------+
| user_id | follower_id |
+---------+-------------+
| steve | demo |
| adam | demo |
| frank | demo |
+---------+-------------+
Help is much appreciated!

You need to join user table twice as
select
u1.user_name as user_name,
u2.user_name as follower_name
from users_followers uf
join users u1 on u1.user_id = uf.user_id
join users u2 on u2.user_id = uf.follower_id

Related

replace the id with the username from an other table in a CROSS JOIN in MySQL

This question is regarding this one: Joining multiple tables to get NOT EQUAL values in MySQL
I want to extend the following query:
SELECT
d.dataid,
d.colors,
u.userid,
u.username
FROM
users u
CROSS JOIN
datas d
WHERE
(u.userid , d.dataid) NOT IN (SELECT
c.userid, c.dataid
FROM
collections c)
AND u.userid = 1
For this data sample:
table datas table users table collections
dataid | colors | addedby userid | username collectionid | userid | dataid
-------------------------- ------------------- ------------------------------
1 | blue | 1 1 | Brian 1 | 1 | 1
2 | red | 1 2 | Jason 2 | 2 | 3
3 | green | 2 3 | Marie 3 | 1 | 3
4 | yellow | 3 4 | 3 | 2
These results are expected:
for Brian
dataid | colors | userid | username
-----------------------------------
2 | red | 1 | Brian
4 | yellow | 1 | Marie
for Jason
dataid | colors | userid | username
-----------------------------------
1 | blue | 2 | Brian
2 | red | 2 | Brian
4 | yellow | 2 | Marie
The row "addedby", which inherits the userid from users, has been added.
At the moment my query replaces the userid from users instead of the addedby from datas with the username.
I really need the userid from datas replaced, not the userid from users. :-)
Does anyone have a clue how to solve this?
Thanks in advance!
cheers
Just join users table once again with datas table. And in the output use username from this join.
SELECT
d.dataid,
d.colors,
uo.userid,
uo.username
FROM
users u
CROSS JOIN
datas d
INNER JOIN
users uo
ON d.added_by = uo.id
WHERE
(u.userid , d.dataid) NOT IN (SELECT
c.userid, c.dataid
FROM
collections c)
AND u.userid = 1
And I believe, that you might even write your query in this way
SELECT u.userid, u.username, d.dataid, d.colors
FROM username u
INNER JOIN datas d
ON u.userid = d.addedby
WHERE d.dataid NOT IN (
SELECT dataid
FROM collections
WHERE userid = 1
)

MySQL mentor student table

This is te problem,
I have a database with a table called Users with 3 columns UserID Mentor and Name, I want to show the users and their mentors. and if i do it like this:
SELECT UserID, mentorID, Name
FROM Users;
I will get this:
| UserID | Mentor | Name
| 1 | NULL | Walter
| 2 | 1 | Jesse
| 3 | 1 | Todd
But I want to get it like this:
| UserID | Mentor | Name
| 1 | NULL | Walter
| 2 | Walter | Jesse
| 3 | Walter | Todd
Thanks for helping,
have a nice day :)
P.S.
The real database a bit more complex but i simplify it here.
You could use a self join ( a join with the same table)
select a.userID, a.Name as user_name , a.mentorID, b.Name as mentor_name
from user a
left join user b on a.mentorID = b.userID
To get the data you need to use self join(A self join is a join in which a table is joined with itself) --
SELECT u.UserID, m.Name AS mentor, u.Name FROM user as u LEFT JOIN user AS m ON u.MentorID = m.UserID

MySQL - Left Outer Join not updating original table

I created a toy dataset where I am trying to count the number of posts for each user. I seem to be getting the correct count values but the count column in the users table is not updated with the values.
I'm new to mysql and very confused! Can somebody tell me what I'm doing wrong?
users:
+---------+------+-------+
| user_id | user | pword |
+---------+------+-------+
| 1 | Amy | abcd |
| 2 | Jess | efgh |
| 3 | Lori | ijkl |
+---------+------+-------+
posts:
+---------+-------------+------+
| post_id | post | user |
+---------+-------------+------+
| 1 | hi | Lori |
| 2 | hello | Jess |
| 3 | hello again | Jess |
| 4 | and again | Jess |
+---------+-------------+------+
mysql> ALTER TABLE users ADD COLUMN post_count INT;
mysql> SELECT u.user_id, COUNT(p.user) AS post_count FROM users u LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id;
+---------+------------+
| user_id | post_count |
+---------+------------+
| 1 | 0 |
| 2 | 3 |
| 3 | 1 |
+---------+------------+
mysql> SELECT * FROM users;
+---------+------+-------+------------+
| user_id | user | pword | post_count |
+---------+------+-------+------------+
| 1 | Amy | abcd | NULL |
| 2 | Jess | efgh | NULL |
| 3 | Lori | ijkl | NULL |
+---------+------+-------+------------+
Thanks!!
Please try the following...
UPDATE users
JOIN ( SELECT u.user_id AS user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id ) postCountFinder
ON users.user_id = postCountFinder.user_id
SET users.post_count = postCountFinder.post_count;
This question takes your list of users and post counts obtained from the following...
SELECT u.user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id;
... and performs an INNER JOIN with Users on shared value of user_id, creating a dataset with every row from users having the corresponding count tacked on the end.
We then use the SET command to set the empty post_count from users to its corresponding joined count.
If you have any questions or comments, thenplease feel free to post a Comment accordingly.
You need update statement to update the value in the newly added column.Try this:
Update usr
set usr.post_count=tbl.post_count
from users usr
inner join
(select u.user_id,COUNT(p.user)
AS post_count FROM users u
LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id ) tbl
on tbl.user_id=usr.user_id

twitter-style follower/following/friend sql query

I'm working on a twitter type of following system. I'm joining two tables, users and followers to get the first and lastname of users who are in the followers table. Then I'm running an inner join on the followers table to capture follower and friend relationships. I'm displaying the results as followers (who follows you), following (who you follow), and friends (mutual following).
With the query below, I'm only able to show the name of the user who wants to see their friends. I'd like to show the FRIENDS of the user, not the user's own name, but can't figure out how to get the users table to do double duty--that is, show me the name of the user and the name of their friend, or just the friend's name.
Thanks.
SELECT users.id, users.firstname, users.lastname, followers.follower_user_id, followers.followee_user_id
FROM users
JOIN followers ON followers.follower_user_id = users.id
INNER JOIN followers ff ON followers.followee_user_id = ff.follower_user_id AND followers.follower_user_id = ff.followee_user_id
I believe that your schema requires a union table to assemble the information you need; and it may be more efficient to do this in multiple tables. To maintain a separate table of followers with (possible) duplicate information from users may also be undesireable. A more efficient schema would be:
mysql> select * from users;
+-----+------------+---------+
| uid | fname | lname |
+-----+------------+---------+
| 1 | Phillip | Jackson |
| 2 | Another | Name |
| 3 | Some Crazy | User |
| 4 | Nameless | Person |
+-----+------------+---------+
4 rows in set (0.00 sec)
mysql> select * from follows;
+---------+-----------+
| user_id | follow_id |
+---------+-----------+
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 2 |
+---------+-----------+
4 rows in set (0.00 sec)
And then your query would look like:
select users.uid,
users.fname,
users.lname,
u.uid,
u.fname,
u.lname from users
inner join follows f on (f.user_id=users.uid)
inner join users u on (u.uid=f.follow_id)
Which returns:
mysql> select users.uid,
-> users.fname,
-> users.lname,
-> u.uid,
-> u.fname,
-> u.lname from users
-> inner join follows f on (f.user_id=users.uid)
-> inner join users u on (u.uid=f.follow_id);
+-----+------------+---------+-----+------------+--------+
| uid | fname | lname | uid | fname | lname |
+-----+------------+---------+-----+------------+--------+
| 1 | Phillip | Jackson | 4 | Nameless | Person |
| 4 | Nameless | Person | 2 | Another | Name |
| 2 | Another | Name | 3 | Some Crazy | User |
| 3 | Some Crazy | User | 2 | Another | Name |
+-----+------------+---------+-----+------------+--------+
4 rows in set (0.00 sec)
SELECT u.id, u.first_name, u.last_name, uf.id, uf.first_name, uf.last_name
FROM users u
JOIN followers f
ON f.follower_user_id = u.id
JOIN followers ff
ON (ff.followee_user_id, ff.follower_user_id) = (f.follower_user_id, f.followee_user_id)
JOIN users uf
ON uf.id = f.followee_user_id

What's the proper way to exclude this group from my MySQL Query?

Table 1: Users
| profile_id | name |
------------------------
| 1 | Joe |
| 2 | Jane |
| 3 | Jill |
| 4 | Jeffery |
Table 2: User and Role to Team Lookup
| team_id | profile_id | role |
---------------------------------
| 1 | 1 | coach |
| 1 | 2 | player |
| 2 | 4 | coach |
| 2 | 1 | player |
The scenario is that Jill is building a team, and the restriction is that you cannot be a player on more than one team. So I'm trying to build a query that pulls up those eligible to join Jill's team.
My first attempt was:
SELECT `users`.`profile_id`
FROM `users` LEFT JOIN `user_role_to_team_lookup` AS `utr` USING(`profile_id`)
WHERE `utr`.`role` != 'player' OR `utr`.`role` IS NULL
The problem is that because Joe is a coach, he matches the criteria~ even though he is also already a player.
What would be the proper way to exclude users that are already players from the result set?
I would write this without the subquery that most people use:
SELECT u.profile_id
FROM users AS u
LEFT OUTER JOIN user_role_to_team_lookup AS utr
ON u.profile_id = utr.profile_id AND utr.role = 'player'
WHERE utr.profile_id IS NULL
In other words, look for a user who is already a player. Those who aren't a player will match no rows in the outer join, and therefore any column of utr will be NULL.
But it's best to put the condition in the ON clause of the join.
SELECT u.profile_id
FROM users u
WHERE NOT EXISTS(SELECT 1
FROM user_role_to_team_lookup urtl
WHERE urtl.profile_id = u.profile_id
AND urtl.role = 'player')
You can probably do this:
SELECT profile_id FROM users
WHERE profile_id NOT IN (SELECT DISTINCT profile_id FROM utr WHERE role = 'player');
SELECT profile_id
FROM users
WHERE profile_id NOT IN (
SELECT profile_id
FROM user_role_to_team_lookup
WHERE role = 'player');