この記事では、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 クラスタ環境構築と分散アプリケーション展開
具体的なupgradeスクリプトは次のとおりです。インデックスを動的に削除する アップグレードが存在...
目次導入スクロール要素.スクロール()要素.scrollHeight/scrollWidth要素.s...
1. よく使われるHTMLタグの最適化HTML は Web 編集者にとって基本的なスキルであるべきで...
目次プロジェクトの背景始めるvue-cliでプロジェクトを作成するモバイル適応についてnormali...
今日は、初心者の次のような質問に答えます。学ぶ勇気さえあれば、自分のウェブサイトを構築するのは簡単で...
目次序文1. リクエスト内容が大きすぎる解決: CDN の紹介リクエストリソースを圧縮する1. HT...
同僚から助けを求められました。バックエンド システムへのログインは成功したものの、システムには正常に...
序文今日 GitHub を閲覧していたところ、SQL を最適化および書き換えるための sora とい...
スロットとは何ですか?スロット ディレクティブは v-slot であり、現在 slot と slot...
色はあらゆるウェブサイトにとって最も重要な要素の 1 つであり、閲覧者に大きな影響を与えるため、色の...
MySQL では、REVOKE ステートメントを使用してユーザーの特定の権限を削除できます (ユーザ...
目次1. インストール2. はじめに3. 使用4. 必要に応じてEChartsチャートとコンポーネン...
Vue コンポーネントをカプセル化する場合でも、機能コンポーネントをクロスファンクショナルに使用しま...
この記事は、4G メモリ システム用の MySQL 構成ファイル ソリューションです (主に Inn...
データベース内のトランザクションとロックを表示するための一般的なステートメントトランザクションの待機...