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
Related
Suppose I have a database with two tables. A "user" table containing an ID, first name, and last name. And a "game" table containing an ID, score, and a userID (the same ID from the user table).
------------------------- ------------------------
ID | fName | lName ID | score | userID
------------------------- ------------------------
1 | Joe | Smith 1 | 20 | 2
2 | Jane | Doe 2 | 15 | 2
3 | Sam | Mills 3 | 18 | 3
-------------------------- 4 | 22 | 3
I want to get ONLY the highest score of each user that has played the game. I want to display something like this:
Jane | Doe | 20
Sam | Mills | 22
Using mysql, how can I achieve this? Also do I need to take into account that there are no records for Joe Smith (userID = 1) in the "game" table?
Thank You to whoever can help!
You can use aggregation and join:
select u.fname, u.lname, gu.max_score
from (select userid, max(score) as max_score
from games g
group by userid
) gu join
users u
on u.id = gu.userid
For achieve this you can use aggregation function and left join:
SELECT U.id, U.fname, U.lname,IFNULL(MAX(G.score),0) AS score FROM user AS U
LEFT JOIN game AS G ON G.userID = U.id
GROUP BY U.id, U.fname, U.lname
I have used left join so if game table don't have any user record so it would be returning 0.
This is DB FIDDLE link you can check same Qry there.
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
)
I have 3 MySQL tables which hold information about a user, coupon information and if a user has used a coupon. Something like this:
User Table
+----------+-------------+
| User_ID | Name |
+----------+-------------+
| 1 | Sarah J |
| 2 | John Smith |
| 3 | Osman Lee |
+----------+-------------+
Coupon Table
+----------+-------------+
| Coupon_ID| Title |
+----------+-------------+
| 1 | Free Stuff |
| 2 | 50% Off |
| 3 | $5 off $25 |
+----------+-------------+
And Redeemed Table
+----------+---------+----------+
| Coupon_ID| User_ID | Redeemed |
+----------+---------+----------+
| 1 | 1 | yes |
| 2 | 2 | yes |
| 1 | 2 | yes |
+----------+---------+----------+
Basically I want this output for every user:
Sarah J: Coupons 2 & 3
John Smith: Coupon 3
Osman Lee: Coupon 1, 2 & 3
I tried using joins but no luck so far. Any suggestions? [For Sarah]
SELECT Coupons.coupon_id, Coupons.title
FROM Coupons
LEFT JOIN Redeemed ON Redeemed.coupon_id != Coupons.coupon_id
WHERE Users.user_id = '1'
The idea is to generate all the rows and then remove the ones that exist -- a cross join and a left join:
select u.user_id, c.coupon_id as not_used_coupon_id
from coupons c cross join
users u left join
redeemed r
on r.coupon_id = c.coupon_id an dr.user_id = u.user_id
where r.coupon_id is null;
You can add the where clause on the users tables.
I assume that every relation between coupon and user is in the Redeemed Table.
If that is the case then you can simply use an inner join like this:
SELECT Coupons.coupon_id, Coupons.title
FROM Redeemed r
INNER JOIN Users u ON
u.User_ID = r.User_ID
INNER JOIN Coupons c ON
c.Coupon_ID = r.Coupon_ID
where r.Redeemed = 'No'
I assume that the value Redeemed can be no for coupons that have not been Redeemed.
I hope that helps...
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
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');