0

我需要删除用户(消息所有者、发件人)必须来自澳大利亚且 21 岁以上的所有公共消息。

我收到一个错误:

***#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 2 行的 'inner join User inner join City inner join Country where Message.messa' 附近使用 ***。

我的文字甚至没有涵盖一半的任务,所以如果有人可以在这里帮助我。

这是我的代码:

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'  
4

2 回答 2

1

试试这个:

当您对两个表进行内部连接时,您必须给出一个条件以在 on 条件下连接两个表。

delete M
 from  Message M inner join User U
    on U.user_id=M.owner_id 
 inner join City C
    on City.city_id=U.city_id 
 inner join Country CT
    on CT.country_id=C.country_id
 where M.message_type=0 
 and   datediff(curdate(),M.posted) >366 
 and   datediff(U.date_of_birth, str_to_date('1/01/1991', '%m/%d/%Y')) < 366
 and   CT.name='Australia'
于 2012-08-21T05:37:59.223 回答
1

这是因为User 它是 MySQL 中的保留关键字,因此您需要用引号将其背光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';

或者,如果您将加入条件放在WHERE子句中,则无需使用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';
于 2012-08-21T05:39:15.800 回答