delete results of a select with a JOIN - mysql

I have a table of users that may or may not have photos uploaded. I want to delete any users that don't have photos. Right now I can get the users with no photos like this:
SELECT a.id FROM users a
LEFT JOIN images b
ON a.id = b.user_id
WHERE b.user_id is null
However, I'm a little stumped on how to actually make the delete happen.
This doesn't work:
DELETE FROM users c WHERE c.id IN ( SELECT a.id FROM users a
LEFT JOIN images b
ON a.id = b.user_id
WHERE b.user_id is null
)
And neither does this:
DELETE FROM users a
LEFT JOIN images b
ON a.id = b.user_id
WHERE b.user_id is null
What's the right way to do a delete on the results of a JOIN?

I am guessing your 2nd example failing because it is referencing the table in the subquery, but it shouldn't actually need to:
DELETE FROM users c WHERE c.id NOT IN ( SELECT DISTINCT user_id FROM images);
Otherwise, I would've done this:
DELETE FROM users
USING users AS u LEFT JOIN images AS i ON u.id = i.user_id
WHERE i.user_id IS NULL;

DELETE users
WHERE NOT EXISTS (
SELECT 1
FROM images i
WHERE users.user_id = i.user_id)

You are almost there with the delete with joins query but need to give the table from which to delete.
Try this:
delete a.* from users a left join images b on a.user_id=b.user_id where b.user_id is null;

Related

How to run nested left join in mysql

I have three tables, likes, users and statuses. So I am returning from likes tables and joining likes table with user table to see who liked it and joining likes table with statuses table to see who posted it.
Now want to get the user information from status table to see who wrote the status. Here comes the problem.
SQL Fiddle http://sqlfiddle.com/#!9/d0707b/2
My current query
select l.*, s.* , a.id as aid, a.userName from likes l
left join
statuses s on l.source_id = s.id
left join
users a on l.user_id = a.id
where
l.user_id in (5,7)
or
(s.privacy='Public' and s.interest in ('mobile', 'andriod') )
order by l.id desc
Here s.user_id=a.id I want to join the statuses table with user table.
[If question is not clear please comment, will try to edit]
Thank you.
You have to make a join to the user table again. Take a look here:
SELECT
l.*, s.*,
a.id AS aid,
a.userName,
b.userName
FROM
likes l
LEFT JOIN statuses s ON l.source_id = s.id
LEFT JOIN users a ON l.user_id = a.id
LEFT JOIN users b ON s.user_id = b.id
WHERE
l.user_id IN (5, 7)
OR (
s.privacy = 'Public'
AND s.interest IN ('mobile', 'andriod')
)
ORDER BY
l.id DESC

MYSQL: Handling Multiple LEFT JOINS

I have a query with one LEFT JOIN that works fine. When I add a second LEFT JOIN to a table with multiple records per field in the first table, however, I am getting the product of the results in the two tables ie books x publishers returned. How can I prevent this from happening?
SELECT a.*,b.*,p.*, group_concat(b.id as `bids`)
FROM authors `a`
LEFT JOIN books `b`
ON b.authorid = a.id
LEFT JOIN publishers `p`
on p.authorid = a.id
GROUP by a.id
EDIT:
Figured it out. The way to do this is to use subqueries as in this answer:
SELECT u.id
, u.account_balance
, g.grocery_visits
, f.fishmarket_visits
FROM users u
LEFT JOIN (
SELECT user_id, count(*) AS grocery_visits
FROM grocery
GROUP BY user_id
) g ON g.user_id = u.id
LEFT JOIN (
SELECT user_id, count(*) AS fishmarket_visits
FROM fishmarket
GROUP BY user_id
) f ON f.user_id = u.id
ORDER BY u.id;
If you do multiple LEFT Joins, your query will return a cartesian product of the results. To avoid this and get only one copy of fields you desire, do a subquery for each table you wish to join as below. Hope this helps someone in the future.
SELECT u.id
, u.account_balance
, g.grocery_visits
, f.fishmarket_visits
FROM users u
LEFT JOIN (
SELECT user_id, count(*) AS grocery_visits
FROM grocery
GROUP BY user_id
) g ON g.user_id = u.id
LEFT JOIN (
SELECT user_id, count(*) AS fishmarket_visits
FROM fishmarket
GROUP BY user_id
) f ON f.user_id = u.id
ORDER BY u.id;

How do I change this query to use a JOIN?

I currently have
select * from A where A.id not in
(select B.id from B where B.user ='b') and A.user <> 'b'
How do I change this query to use a JOIN -- something like LEFT JOIN user AS last_replied_user ON topic.last_replied_by = last_replied_user.uid
select * from users u
left outer join topic t on u.id=t.last_replied_by
where t.last_replied_by IS NULL
This will get you all users where the user id is not found in the last replied by column in the topics table.

chat application friend list

I have 3 MySQL tables namely chat_comments, chat_friends and user_details and I want to display a friend list.
My tables:
chat_comments(comment_id,comment,user_id,user_id2,date_added)
chat_friends(user_id,user_id2,approved)
user_details(user_id, mainimage_id, fullname)
To do this, I need a query that will return the needed fields (u.mainimage_id, u.fullname, b.comment, b.user_id) so I can loop through the list to display a table.
SQL so far (help from #Andriy M):
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments WHERE user_id=2020 OR user_id2=2020
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
The above query returns the last comment made by the logged-in user's friends in conversation not the last comment between the logged-in user and his/her friend regardless of who made it.
I would like it to return the last comment between the logged-in user and their friend individually regardless of who made it. In the chat_messages table, user_id is the sender and user_id2 is the receiver. Hope it makes sense?
Like #imm said in a comment, you need to use an outer join. In case of a left join, the user_details table should become the left side of the join, the right side being the result of your inner join of chat_comments with your a derived table. You'll also need to remove the user_id IN (…) condition from inside the a subselect and re-apply it to the user_details table. Here:
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
;
Alternatively, you could use a right join. In this case you would just need to move the user_id IN (…) condition, similarly to the LEFT JOIN solution above, and replace the second INNER JOIN with RIGHT JOIN:
SELECT
cc.comment, cc.date_added, u.fullname, u.mainimage_id
FROM
(
SELECT user_id, MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a
INNER JOIN
chat_comments cc ON
a.user_id = cc.user_id AND
a.maxcomment = cc.comment_id
RIGHT JOIN
user_details u ON
a.user_id = u.user_id
WHERE u.user_id IN (select user_id2 from chat_friends where user_id=2020 AND approved=1)

Make SQL query from four table

My database have got 4 table :
users
id
username
images
id
user_id
image
user_follow
user_id
user_follow
commentaries
image_id
text
I try to make a query to get all my and my friends pictures
1) row must be shown only if user have picture in "images" table
2) only image from me and my friends (depending on "user_follow" table)
3) count commentaries for each picture
my query is:
SELECT u.username as user, i.image as user_image, p.image, UNIX_TIMESTAMP(p.date) as date, COALESCE ( imgcount.cnt, 0 ) as comments
FROM users u
LEFT JOIN user_follow f ON u.id = f.follow_id
LEFT JOIN images p ON p.user_id = u.id
LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
LEFT JOIN (SELECT image_id, COUNT(*) as cnt FROM commentaries GROUP BY image_id ) imgcount ON p.id = imgcount.image_id
WHERE f.user_id = 3 OR p.user_id = 3
ORDER BY p.date DESC
Commentaries count of each image sql line works fine in this query
LEFT JOIN (SELECT image_id, COUNT(*) as cnt FROM commentaries GROUP BY image_id ) imgcount ON p.id = imgcount.image_id
in this line I try to get user his last upload image as avatar from "images" table
LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
This query dot not return result correctly because it show my friends who do not have pictures and something is not good with my own pictures (in database I have 2 row with my user_id : 3 ) but sql return 4
If I'm understanding correctly, it seems like you've made this a lot more complex than it needs to be. The reason you are getting records for your friends without pictures is because you are using a LEFT JOIN. Change that to an INNER JOIN and you will only get the records that match the join condition.
I think you are looking for something like the following. You'll need to make a few tweaks but hopefully this helps.
SELECT u.username as user, i.image as user_image, count(*) as comments
FROM users u
INNER JOIN user_follow f ON u.id = f.follow_id
INNER JOIN images p ON p.user_id = u.id
INNER JOIN commentaries c ON c.image_id = p.id
WHERE u.user_id = 3
GROUP BY u.username, i.image;