简体   繁体   中英

Finding and deleting one record from two tables ? MySQL PHP

I have two MySQL tables, one with the field Username and the other User_Name, is it possible to find a record in one of the tables and delete it? I've tried the following but it didnt work...

DELETE FROM JB_Employer, JB_Jobseeker WHERE Username='$userName' OR User_Name='$userName'

I feel as though I'm missing something obvious..

Thanks. Kai

There is a difference in syntaxes when FROM is used before or after the table names from which rows are to be deleted. These should work for deleting entries from both tables:

Using FROM after the table-names:

DELETE JB_Employer, JB_Jobseeker 
FROM JB_Employer INNER JOIN JB_Jobseeker ON JB_Employer.Username = JB_Jobseeker.User_Name
WHERE JB_Employer.Username= '$userName';

Using FROM before the table-names:

DELETE FROM JB_Employer, JB_Jobseeker 
USING JB_Employer INNER JOIN JB_Jobseeker ON JB_Employer.Username = JB_Jobseeker.User_Name
WHERE JB_Employer.Username= '$userName';

EDIT 1

The below section explains the queries for deleting the entries from both tables or either table.

Using FROM after the table-names:

DELETE JB_Employer, JB_Jobseeker 
FROM JB_Employer
LEFT JOIN JB_Jobseeker ON JB_Employer.Username = JB_Jobseeker.User_Name
WHERE JB_Employer.Username= '$userName';

Using FROM before the table-names:

DELETE FROM JB_Employer, JB_Jobseeker 
USING JB_Employer
LEFT JOIN JB_Jobseeker ON JB_Employer.Username = JB_Jobseeker.User_Name
WHERE JB_Employer.Username= '$userName';

Hope it works!

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