この記事では、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 クラスタ環境構築と分散アプリケーション展開
mha4mysql をインストールする場合の手順は、おおよそ次のようになります: unzip、per...
インストール環境: CentOS7 64ビットMINI版、MySQL5.7をインストール1. YUM...
目次1. podmanの紹介2. Dockerと比較した利点3. 互換性4. バックグラウンド サー...
この記事では、HTTP プロトコルのリファラーのメタデータ パラメータの提案について説明します。この...
フォームのアクションは URL ジャンプとは異なります。フォームはバックグラウンドにデータを渡すこと...
序文インデックスを追加した場合と追加しなかった場合の違いを反映するには、数百万のデータを使用する必要...
MySQLにおける静的変数の役割の詳細な説明静的変数の使用 静的変数サンプルコード: 関数テスト()...
私たちの生活、仕事、勉強において、ソーシャル ネットワークは徐々に将来のインターネット発展のトレンド...
目次1. フィルター() 2. 各() 3. いくつか() 4. すべて() 5. 減らす() 6....
Vueはブール値でストレージを保存します今日、問題に遭遇しました。バックグラウンドから返された真偽の...
1. 仮想マシンに共有フォルダを設定します。 1. 処理する仮想マシンを選択し、右クリックして設定...
以下のコードをDreamweaverのコードエリアにコピーすると、プレビュー時に以下の画像が表示され...
目次序文なぜユニットテストを導入するのですか?ユニットテストの概要テスト開発パターン1. テスト駆動...
目次序文配列.プロトタイプ.includes文法パラメータ戻り値例配列プロトタイプの削減文法パラメー...
多くの場合、Linux システムに Web サービス アプリケーション (Tomcat、Apache...