简体   繁体   中英

sql delete id's from two tables

MySQL version 5.5.37 using phpmyadmin

I have a problem with deleting the ID's from 2 tables.

The tables are jos_users and jos_vm_user_info

I need to delete the id's from both tables using the 'registered before' date and 'last visit' date in jos_users

If I run this -

DELETE jos_users,jos_vm_user_info from jos_users,jos_vm_user_info WHERE jos_users. `registerDate` < '2012-12-31 23:59:59' AND `lastvisitDate` < '2012-12-31 23:59:59'

I get '0 rows deleted'. I would expect over 2000 rows to be deleted

I've obviously got something wrong there but I don't enough about what I'm doing

or is there a better way to do it?

BTW a SELECT using the table and conditional statements works fine

SELECT * FROM `jos_users` WHERE  `registerDate` < '2012-12-31 23:59:59' AND `lastvisitDate` < '2012-12-31 23:59:59' 

It's getting the id's deleted in the the jos_vm_user_info table that's the problem.

You have incorrect DELETE syntax..

example of delete syntax:

DELETE FROM tbl_name WHERE 1>0;

Your delete syntax contain part DELETE some_column FROM....

that highlighted part is wrong..

http://dev.mysql.com/doc/refman/5.6/en/delete.html

Look up Multi-Table Deletes on that page.

You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the condition in the WHERE clause.

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

Thanks everyone for the answers, much appreciated. It all helps me to understand sql better. I realised that the best way to achieve the result I want, which is to remove fake users, was to remove all id's that were not in the orders table. I found the answer here Delete sql rows where IDs do not have a match from another table I removed 6000 id's from a total of 10,000 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM