#author("2023-03-11T09:14:30+00:00","default:hotate","hotate") #author("2023-05-27T12:05:59+00:00","default:hotate","hotate") #contents &tag(SQL,データベース); * 情報 [#h8171e05] ** DELETE [#j2e350a5] - [[[mysql]MySQLはUPDATE文やDELETE文にORDER BY句やLIMIT句を使えるが、使わないほうがいい | ごみぶろぐ>https://gomiryo.com/%E5%BE%92%E7%84%B6%E8%8D%892-0/mysqlmysql%E3%81%AFupdate%E6%96%87%E3%82%84delete%E6%96%87%E3%81%ABorder-by%E5%8F%A5%E3%82%84limit%E5%8F%A5%E3%82%92%E4%BD%BF%E3%81%88%E3%82%8B%E3%81%8C%E3%80%81%E4%BD%BF%E3%82%8F%E3%81%AA%E3%81%84/]] 結論から言うと、MySQLではDELETE文やUPDATE文にORDER BY句やLIMIT句を使うことができる。つまり、実行した結果をソートした順のレコードへ、DELETEやUPDATEの処理を適用することが可能ということだ。 これはSQL標準におそらくないものでありMySQL独自の拡張構文であり(軽く調べた限り)PostgreSQLやOracleでは使えない。 -- [[python - Syntax Error in SQLite Near ORDER (sqlite3.OperationalError) - Stack Overflow>https://stackoverflow.com/questions/71639809/syntax-error-in-sqlite-near-order-sqlite3-operationalerror]] Delete from fremdgehen where id IN (Select id from fremdgehen limit 1); ** WHERE [#w91531a8] - [[[SQL] Where句とHaving句の違い | DevelopersIO>https://dev.classmethod.jp/articles/difference-where-and-having/]] SQLが実行される順序は以下のようになっており FROM → WHERE → GROUPBY → HAVING → SELECT → ORDERBY GroupByでグルーピングする前に抽出するのがWhere句で GroupByでグルーピングした後に抽出するのがHaving句になります。 ** [[日時]] [#a5efef22] - 日付別の件数で集計する。 select cast(created_at as date) as day,count(*) from comments group by day order by day asc * ツール [#r7f9a0df] + [[Execute Query>http://executequery.org/index.jsp]] * 関連 [#jdd1dfa6] - [[SQL/Tips]] #taglist(tag=SQL)