I need to delete all public messages older than a year where Users (message owners, senders) must be from Australia and 21 years old.
I get an error:
***#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join User inner join City inner join Country where Message.messa' at line 2***.
My texts do not cover even a half of the task, so if anyone could help me here.
Here is my code:
delete
from Message
inner join User
inner join City
inner join Country
where Message.message_type=0 and
datediff(curdate(),Message.posted) >366 and
User.user_id=Message.owner_id and
datediff(User.date_of_birth,
str_to_date('1/01/1991', '%m/%d/%Y')) < 366 and
City.city_id=User.city_id and
Country.country_id=City.country_id and
Country.name='Australia'
this is because User
is a reserved keyword in MySQL so you need to back lit it with quotes User
:
DELETE Message
FROM Message
INNER JOIN `User`
ON `User`.user_id = Message.owner_id
INNER JOIN City
ON City.city_id = `User`.city_id
INNER JOIN Country
ON Country.country_id = City.country_id
WHERE Message.message_type = 0 AND
DATEDIFF(CURDATE(), Message.posted) > 366 AND
ROUND(DATEDIFF(CURDATE(), `User`.date_of_birth)/365) < 21 AND
Country.name = 'Australia';
alternatively if you are putting joining condition in WHERE
clause then no need to use INNER JOIN
:
delete Message
from Message, `User`, City, Country
where `User`.user_id=Message.owner_id and
City.city_id=`User`.city_id and
Message.message_type=0 and
Country.country_id=City.country_id and
datediff(curdate(),Message.posted) >366 and
datediff(`User`.date_of_birth,
str_to_date('1/01/1991', '%m/%d/%Y')) < 366 and
Country.name='Australia';