この記事では、MySQL で複数のテーブルを使用して 1 対多のクエリを使用して最新のデータを取得する方法を例を使って説明します。ご参考までに、詳細は以下の通りです。 最新のデータを取得するためのMySQLマルチテーブル関連付け1対多クエリ発生した問題複数のテーブルが1対多のクエリに関連付けられ、最新のデータを取得し、データが繰り返される
`customer` が存在する場合はテーブルを削除します。 テーブル「顧客」を作成します( `id` BIGINT NOT NULL COMMENT '顧客ID', `real_name` VARCHAR(20) NOT NULL COMMENT '顧客名', `create_time` DATETIME NOT NULL COMMENT '作成時刻', 主キー (`id`) )ENGINE=INNODB DEFAULT CHARSET = UTF8 COMMENT '顧客情報テーブル'; -- テーブル customer のデータ `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7717194510959685632', '张三', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7718605481599623168', '李四', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7720804666226278400', '王五', '2019-01-23 16:23:05') を挿入します。 INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7720882041353961472', '刘六', '2019-01-23 16:23:05'); `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722233303626055680', 'Baby', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722233895811448832', 'Xiaobao', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722234507982700544', '大宝', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722234927631204352', '二宝', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722235550724423680', '小贱', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722235921488314368', 'Xiao Ming', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722238233975881728', '小黑', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722246644138409984', 'Xiaohong', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722318634321346560', '阿狗', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722318674321346586', '阿娇', '2019-01-23 16:23:05') を挿入します。 `demo`.`customer` (`id`, `real_name`, `create_time`) に VALUES ('7722318974421546780', '阿猫', '2019-01-23 16:23:05') を挿入します。 `order_info` が存在する場合はテーブルを削除します。 テーブル「order_info」を作成します( `id` BIGINT NOT NULL COMMENT '注文ID', `industry` VARCHAR(255) デフォルト NULL コメント 'Industry', `nature_tax` VARCHAR(255) デフォルト NULL コメント '税金の性質', `customer_id` VARCHAR(20) NOT NULL COMMENT '顧客ID', `create_time` DATETIME NOT NULL COMMENT '作成時刻', 主キー (`id`) )ENGINE=INNODB DEFAULT CHARSET = UTF8 COMMENT '注文情報テーブル'; -- テーブル order_info のデータ `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700163609453207552', 'catering and hotel', 'small scale', '7717194510959685632', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700163609453207553', 'catering and hotel', 'small scale', '7717194510959685632', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700167995646615552', 'High-tech', 'General taxpayer', '7718605481599623168', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700167995646615553', 'Commerce', 'General Taxpayer', '7718605481599623168', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700193633216569344', 'Commerce', 'General Taxpayer', '7720804666226278400', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700193633216569345', 'High-tech', 'General taxpayer', '7720804666226278400', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700197875671179264', 'catering and hotel category', 'general taxpayer', '7720882041353961472', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7700197875671179266', 'catering and hotel category', 'general taxpayer', '7720882041353961472', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7703053372673171456', 'ハイテク', '小規模', '7722233303626055680', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7703053372673171457', 'ハイテク', '小規模', '7722233303626055680', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709742385262698496', 'サービス カテゴリ', '一般納税者', '7722233895811448832', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709742385262698498', 'サービス カテゴリ', '一般納税者', '7722233895811448832', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745055683780608', 'ハイテク', '小規模', '7722234507982700544', '2019-01-23 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745055683780609', 'インポートとエクスポート', '小規模', '7722234507982700544', '2019-01-23 17:09:53') に INSERT INTO します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745249439653888', '文化・スポーツ', '一般納税者', '7722234927631204352', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745249439653889', 'High-tech', 'General taxpayer', '7722234927631204352', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745453266051072', 'ハイテク', '小規模', '7722235550724423680', '2019-01-24 16:54:25') を挿入します。 INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745453266051073', '文化・スポーツ', '小規模', '7722235550724423680', '2019-01-23 17:09:53'); `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745539848413184', 'Technology', 'General Taxpayer', '7722235921488314368', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745539848413185', 'High-tech', 'General taxpayer', '7722235921488314368', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745652603887616', 'High-tech', 'General taxpayer', '7722238233975881728', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745652603887617', 'Technology', 'General Taxpayer', '7722238233975881728', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745755528568832', 'Import and Export', 'General Taxpayer', '7722246644138409984', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745755528568833', '教育コンサルティング', '小規模', '7722246644138409984', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745892539047936', 'Education Consulting', 'General Taxpayer', '7722318634321346560', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709745892539047937', 'Import and Export', 'General Taxpayer', '7722318634321346560', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709746000127139840', 'Production', 'Small Scale', '7722318674321346586', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709746000127139841', 'Agriculture', 'General Taxpayer', '7722318674321346586', '2019-01-23 17:09:53') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709746447445467136', 'Agriculture', 'General taxpayer', '7722318974421546780', '2019-01-24 16:54:25') を挿入します。 `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) に VALUES ('7709746447445467137', 'Production', 'Small Scale', '7722318974421546780', '2019-01-23 17:09:53') を挿入します。
order_info を更新し、create_time を NOW() に設定します。
選択 クレジットID、 cr.実名、 oi.industry、 oi.自然税 から 顧客AS cr 左結合( a.industry、a.nature_tax、a.customer_id、a.create_timeをorder_infoから選択します。 左結合( SELECT MAX(create_time) AS create_time, customer_id FROM order_info GROUP BY customer_id ) AS b ON a.customer_id = b.customer_id ここで、a.create_time = b.create_time ) AS oi ON oi.customer_id = cr.id GROUP BY cr.id;
選択 クレジットID、 cr.実名、 oi.industry、 oi.自然税 から 顧客AS cr 左結合( a.industry、a.nature_tax、a.customer_id、a.create_timeをorder_infoから選択します。 左結合( SELECT MAX(create_time) AS create_time, customer_id FROM order_info GROUP BY customer_id ) AS b ON a.customer_id = b.customer_id ここで、a.create_time = b.create_time ) AS oi ON oi.customer_id = cr.id cr.id でグループ化し、COUNT(cr.id) >= 2 にします。
選択 クレジットID、 cr.実名、 oi.industry、 oi.自然税 から 顧客AS cr 左結合( a.industry、a.nature_tax、a.customer_id、a.create_timeをorder_infoから選択します。 左結合( SELECT MAX(id) AS id, customer_id FROM order_info GROUP BY customer_id ) AS b ON a.customer_id = b.customer_id ここで、a.id = b.id ) AS oi ON oi.customer_id = cr.id;
MySQL 関連のコンテンツに興味のある読者は、このサイトの次のトピックをチェックしてください: 「MySQL クエリ スキル」、「MySQL トランザクション操作スキル」、「MySQL ストアド プロシージャ スキル」、「MySQL データベース ロック関連スキルの概要」、および「MySQL 共通関数の概要」 この記事が皆様のMySQLデータベース設計に役立つことを願っています。 以下もご興味があるかもしれません:
|
<<: jsは双方向データバインディング(アクセサ監視)を実現します
>>: Docker+K8S クラスタ環境構築と分散アプリケーション展開
「downloadlaod」を直接クリックしてもダウンロードできない場合は、ここからダウンロードす...
IE6 と IE7 では CSS の解釈に多くの違いがあります。今日はそのうちの 1 つである高さに...
広告業は人間であることに似ていると言う人がいます。これは本当です。優れた広告には、優れた人間と同じよ...
jQuery を使用してフェードインとフェードアウト効果を完成させる前に、まずいくつかのコードを理解...
この記事の例では、カウントダウン効果を実現するためのJavaScriptの具体的なコードを参考までに...
XPath は、XML ドキュメントの一部を選択するための言語です。 XPath は、XSLT、XQ...
1. PRIMARY KEY(主キーインデックス)を追加するmysql>ALTER TABLE...
4 つのネットワーク タイプ:なし: コンテナのネットワーク機能を一切設定しません。--net=no...
多くの Web ページにはナビゲーション バーに小さな三角形があり、この機能を実装するのは実は非常に...
この記事では、:placeholder-shown 疑似クラスを使用して、純粋な CSS で浮動疑問...
目次1. Ubuntuソースの変更2. MySQLをインストールする3. 新しいユーザーを作成し、権...
目次はじめにNginx Dockerファイル新しい会議もっと参考文献はじめに最近、アプリケーションの...
古典的な色の組み合わせは力と権威を伝え、強いロイヤルブルーはあらゆる古典的な色の組み合わせの中心的な...
目次質問伸ばす問題を解決する要約する質問プロジェクトの要件に従って、以下の州地図で個々の都市を(異な...
Traceroute を使用すると、情報がコンピュータからインターネットの反対側のホストまでたどるパ...