shimgo
7/1/2018 - 6:34 AM

MySQLでサブクエリっぽく削除する

MySQLのDELETE文ではexistsやサブクエリを使えないので複数DELETE構文を使う。
下記は重複したデータのうち古い方を消すクエリ

delete target from user_companies target, user_companies sub 
where 
  target.user_id = sub.user_id 
  and target.company_id = sub.company_id 
  and target.send_permitted = sub.send_permitted 
  and target.created_at < sub.created_at;