0

ユーザー (メッセージの所有者、送信者) がオーストラリア出身で 21 歳でなければならない、1 年以上前のパブリック メッセージをすべて削除する必要があります。

エラーが発生します:

***#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

これを試して:

2つのテーブルで内部結合を行う場合、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 に答える