Deleting rows when using a join

What is the best way to delete when a join is involved?

Thanks!

SELECT
u.uID,
s.searchDate,
u.last,
s.keyword,
s.alerts,
s.lastAlert,
s.clicks,
s.lastClick,
s.timeStamp
FROM users u
INNER
JOIN searches s
ON u.uID = s.author
WHERE
(DATE_SUB(CURDATE(),INTERVAL 1825 DAY) > u.last) and clicks < 4
ORDER BY last
limit 1000;

select the ID of the row and then use ‘DELETE … WHERE id =…’

My join query produces hundreds of rows right now. I am seeking a solution that will allow me to delete them all at once. Thanks though.

DELETE FROM … WHERE id IN (select t1.id LEFT JOIN … ON … WHERE …)

something like that should work

the SQL to do deletions using a join actually varies a great deal from one database system to another

here at sitepoint, we have a databases forum and a mysql forum, so since you did not post in the mysql forum, could you tell us which database system you’re running

mySQL

Thread moved to MySQL forum

Ok r937, I’m ready for your assistance.

:slight_smile:

could you start by explaining which rows from which table you want to delete

you’ve got users and searches

but you never said which one you want to delete

Sorry about that. I’m wanting to delete from the searches table. I’m using the users table to see how long its been since they last logged in.

Thanks!

DELETE searches
  FROM users
INNER
  JOIN searches
    ON searches.author = users.uID 
   AND searches.clicks < 4
 WHERE users.last < CURRENT_DATE - INTERVAL 1825 DAY

r937, excellent and thank you again.