この記事では、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 クラスタ環境構築と分散アプリケーション展開
導入増分バックアップとは、完全バックアップまたは最後の増分バックアップの後、後続の各バックアップでは...
1. ウィンドウ -> 設定を選択してEclipseの設定パネルを開きます。 2. 「設定」ウ...
MySQLインストールチュートリアル、参考までに具体的な内容は次のとおりです。 1. ダウンロードY...
1. VMware 15.5から新しい仮想マシンを作成する1. VMware を開き、ホームページで...
1. 問題の再現:各日の合計数を日ごとにカウントします。データのない日がある場合、グループ化によっ...
目次1. 簡単な説明2. クラスターを作成する手順2.1. ディレクトリを作成する2.2. ソースコ...
質問:最近、プロジェクトの統計を行っていたときに、テーブルを上下にスクロールしたときにテーブルの先頭...
目次更新可能なビュービューのパフォーマンスビューの制限ビューは MySQL 5.0 以降で導入されま...
MySQL 5.7.20 zipインストール、具体的な内容は次のとおりです(1)圧縮パッケージを解凍...
序文:この記事では主にMySQLのロングトランザクションに関する内容を紹介します。例えば、トランザク...
クリック後にポップアップボックスを実現し、上下左右に中央揃えし、灰色の透明マスクを追加してウィンドウ...
3日間さまざまな困難に遭遇した後、ようやくこの機能を実現しました。正常に実装できる方法を見つける前に...
この記事では、MacOSでのMySQL 8.0.18のインストールと成功したコマンドライン操作を記録...
ReactRouterの実装ReactRouterはReactのコアコンポーネントです。主にReac...
コードは次のようになります。 <!DOCTYPE html> <html> ...