MySQL テーブルから最新の日付付きレコードを取得する方法
時間に依存するデータを MySQL テーブルに保存する場合、多くの場合、次のことが必要になります。フィールドの特定の組み合わせについて最新のレコードを取得します。次の例は、タイムスタンプ、メソッド、ID などのフィールドを備えた RPC 応答のテーブルを処理するときにこれを実現する方法を示しています。
Question
という名前のテーブルについて考えます。次の構造を持つ rpc_responses:
CREATE TABLE rpc_responses ( timestamp DATE, method VARCHAR, id VARCHAR, response MEDIUMTEXT, PRIMARY KEY (timestamp, method, id) );
次の場合データ:
timestamp | method | id | response |
---|---|---|---|
2009-01-10 | getThud | 16 | "....." |
2009-01-10 | getFoo | 12 | "....." |
2009-01-10 | getBar | 12 | "....." |
2009-01-11 | getFoo | 12 | "....." |
2009-01-11 | getBar | 16 | "....." |
望ましい結果
メソッドと ID の既存のすべての組み合わせに対する最新の応答のリストを取得します:
timestamp | method | id | response |
---|---|---|---|
2009-01-10 | getThud | 16 | "....." |
2009-01-10 | getBar | 12 | "....." |
2009-01-11 | getFoo | 12 | "....." |
2009-01-11 | getBar | 16 | "....." |
回答
これを達成するには、次のクエリを利用します:
SELECT * FROM ( SELECT *, IF(@last_method = method, 0, 1) AS new_method_group, @last_method := method FROM rpc_responses ORDER BY method, timestamp DESC ) AS t1 WHERE new_method_group = 1;
このクエリは、結合を必要とせずに、目的の結果を効率的に取得します。また、必要な行構造も維持し、メソッドと ID の一意の組み合わせごとに個別のレコードを提供します。
以上がMySQL でメソッドと ID の組み合わせごとに最新のレコードを選択する方法は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

todropaviewinmysql、 "dropviewifexistsview_name;" andtomodifyaviewを使用して、 "createorreplaceviewview_nameasselect ..."を使用します

mysqlviewscanefectiveativeativeizedesignpatternslikeadapter、decorator、factory、andobserver.1)adapterpatternadaptsdatafromdifferenttablesintoaunifiedview.2)decoratorpatternenhancesdatawithedfieldsfieldsiffieldsiffieldsiffiedを

viewsinmysqlarebenefentialforsimprifiningcomplexqueries、拡張セキュリティ、ダタコンシーニング、および最適化されたパフォーマンスを保証する1)itsmplifyififycomplexqueriesbyencapsulsingthemintoreusableviews.2)viewsencurationecuritybycontrollingcescesces.3)

to CreateAsimpleviewinmysql、usethecreateviewstatement.1)DefinetheTheTheThecreateview_nameas.2)SpecifyTheSelectStatementtatementtatementtatementtatementtatementtatementedeSireddata.3)

tocleateusersinmysql、usethecreateuserstatement.1)foralocaluser:createUser'localuser '@' localhost'identifidedifiedifiedified 'securepassword';

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 中国語版
中国語版、とても使いやすい
