Need a MySQL Query out of this diagram - mysql

I'd like a query that shows me the tracks that a user doesn't have in his playlist. The only parameter I'll be getting is the username of a user.
Is that possible with this diagram? If so, could you provide me with a query?
I've tried a query, but not so succesfull.
SELECT * FROM TRACK
INNER JOIN USER u on u.ID = TRACK.USERID
INNER JOIN Playlist p on p.TrackID = TRACK.ID
WHERE (u.ID IN (SELECT UserID FROM Playlist WHERE UserID = (SELECT ID FROM User WHERE username = 'Arjan')))
AND (TRACK.ID NOT IN (SELECT ID FROM TRACK))
Not sure what's missing here. Maybe I'm just thinking too complicated.

I didn't get a chance to try it out actually. But your query should be something like this one --
Declare #UserName varchar(255)
Set #UserName = N'Arjun'
SELECT U.ID
,U.UserName
,P.ID
,T.Id
,T.TrackUrl
,T.CreationDate
FROM TRACK T
INNER JOIN USER U ON U.ID = T.UserID
LEFT JOIN Playlist P ON P.TrackID = T.ID
AND P.UserID = U.ID
WHERE U.ID IN (
SELECT DISTINCT ID
FROM User
WHERE UserName = #UserName
)
AND P.ID IS NULL;

Related

MySQL SELECT INNER JOIN mistaken

I am trying to retrieve the 'Roles' sets of a given users.id on a query with INNER JOIN combined with a WHERE condition. But things go wrong.
My database has four tables:
t_users : id, username, userpass, status, ...
t_action: id, id_user, id_role, id_type_role, ...
t_role: id, libelle, status
t_type_role: id, libelle, status
My query:
SELECT U.id AS ID, R.libelle AS ROLE, T.libelle AS TYPE
FROM t_user U
JOIN t_action A ON A.id_user = U.id
JOIN t_type_role T ON T.id = A.id_type_role
JOIN t_role R ON R.id = A.id_role
WHERE A.id_user = '1' AND R.libelle = 'System'
But this query returns no data. (Tested on a phpmyadmin SQL board.)
Use:
SELECT u.id AS id,
r.libelle AS role,
t.libelle AS type
FROM users u
JOIN action a ON a.id_user = u.id
JOIN type_role t ON t.id = a.id_type_role
JOIN role r ON r.id = a.id_role
WHERE a.id_user =1
AND t.libelle = 'System';
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=20570938deb2bec281d5070dd28bf19d
Don't put single quotes on integers, change WHERE a.id_user ='1' to WHERE a.id_user = 1.
libelle = 'System' is in the type_role table not in the role table
as Akina has already mentioned in the comment section, there is no "libelle" value in the table "role" which equals 'Système' as you has mentioned it above. That is the reason why you do not get any output. Fix it to 'System' in the MySQL database and try it out again.

Checking result count

I am working on a project that requires getting posts from my database. I am trying to get the posts that are posted by the user and other users unless they are private.
My SQL query is
SELECT . . .,COUNT(F.user_id) AS is_following FROM posts AS P INNER JOIN users AS U ON U.id = P.user_id LEFT JOIN followers AS F ON F.user_id = :userid AND F.following_id = U.id WHERE *If matches search* AND U.private = 0 OR U.id = :userid OR is_following = 1 ORDER BY. . .
I am selecting where the user is following the posters and counting it if 0 it means they are not following and 1 would mean 1 result, therefore, following them. Also I am checking if private is 0 meaning the account is public.
The problem is while I have the is_following = 1 it returns no results if I remove it though it does return results but not all.
Tables:
users: id|username|private|...
followers: userid|followingid
posts: id|userid|body
Content:
users:
user1|test1|1 (This is the user running the request)
user2|test2|1
user3|test3|1
followers:
user1|user2 (User searching follows test2)
posts:
post1|user1|HelloWorld
post2|user2|Hello_World
post3|user3|Hello-World
The expected outcome is to return post1 and post2 but not post3 since user3 is private (private is set to true/1) and user1 does no follow test3, but it should display user2's post since user1 follows user2
If I understand correct.
There are two kinds of groups
private is 0 meaning the account is public
private is 1 need to find the following User on followers table.
A simple way you can make two query one is for private is 0 , another is for private is 1 ,then use UNION ALL combine those.
SELECT * FROM
(
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0
UNION ALL
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
RIGHT JOIN followers AS F ON F.followingid = u.id
WHERE u.private = 1 AND f.userid = :userid
)t
Or use exists to check it
SELECT p.*
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0 OR exists
(
SELECT 1
FROM followers AS F LEFT JOIN users ON F.followingid = users.id
WHERE u.id = users.id
and users.private
and f.userid = :userid
)
sqlfiddle:http://sqlfiddle.com/#!9/50ef05/1
EDIT
if you want to get the user is or isn't is_following,you could follow like this.
then you could find which user have been is_following on outer where clause .
SELECT * FROM
(
SELECT p.*,0 as 'is_following'
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
WHERE u.private = 0
UNION ALL
SELECT p.*,1
FROM users as u
INNER JOIN posts AS P ON p.userid = u.id
RIGHT JOIN followers AS F ON F.followingid = u.id
WHERE u.private = 1 AND f.userid = 'user1'
)t
/*where is_following = 1*/
sqlfiddle:http://sqlfiddle.com/#!9/b7f6c/10
The reason it returns no results when you have the is_following condition is that you are trying to use an alias in a WHERE clause which is not permitted by MySQL. Replace is_following with COUNT(F.user_id) and it should work.

SQL query to link two tables returning error

I have two tables as follows
user [ ID , username ]
relationship [ user1_id(FK) , user2_id (FK) , status ]
I am trying to get the username by using either user1_id or user2_id where the status = 1 from relationship table. user1_id and user2_id are both IDs from the user table. The following query is failing and I am not sure where it's going wrong.
SELECT
U.username,
(R.first_user_id, R.second_user_id AS friends)
FROM
user U,
`relationship` R
WHERE (R.`first_user_id` = {$userID} OR R.`second_user_id`)
AND (`status` = 1 AND U.ID = friends)
returns both names of users in a relationship with a status of 1.
this also assumes that if a relationship record exists, both users must be in the user table.
SELECT U1.UserName, U2.username
FROM Relationship R
INNER JOIN USER U1
on R.User1_ID = U1.user_ID
INNER JOIN USER U2
and R.User2_ID = U2.user_ID
WHERE R.Status=1
It looks like you may be trying to get the usernames of all users that have a relationship with a certain specified user, regardless of the order of user IDs in the relationship record. That could be this:
SELECT
U.username,
U.first_user_id,
FROM
user U
JOIN `relationship` R
ON R.first_user_id = U.ID
WHERE
(R.`second_user_id` = {$userID})
AND (`status` = 1)
UNION ALL
SELECT
U.username,
U.second_user_id,
FROM
user U
JOIN `relationship` R
ON R.second_user_id = U.ID
WHERE
(R.`first_user_id` = {$userID})
AND (`status` = 1)
If that produces duplicates (or could do) and you don't want it to do, then change the UNION ALL to a straight UNION.
I have succeeded based on xQbert answer:
SELECT
U1.username,
U2.username
AS user_friend
FROM
Relationship R
INNER JOIN
user U1
ON
R.first_user_id = U1.ID
INNER JOIN
user U2
ON
R.second_user_id = U2.ID
WHERE (R.`first_user_id` = {$userID} OR R.`second_user_id` = {$userID})
AND `status` = 1

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

MySQL: delete from IN

I know this is a simple syntax issue. Trying to delete all users from a subquery:
delete from users
where id IN (
select u.id
from users u
where not exists (select * from stickies i where i.user_id = u.id)
group by u.email
having count(*) > 1
)
Getting this error:
error : You can't specify target table 'users' for update in FROM clause
The subquery works fine (returns list of user id's).
DELETE u.*
FROM users u JOIN (
SELECT u.id
FROM users u LEFT JOIN stickies i ON i.user_id = u.id
WHERE i.user_id IS NULL
GROUP BY u.email
HAVING COUNT(*) > 1
) r ON r.id = r.id
Note: in the inner query, you are grouping by email, but selecting a user ID. this may return non deterministic results.