この記事では、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 クラスタ環境構築と分散アプリケーション展開
Linux に MySQL をインストールするには、yum インストールとソース コード コンパイ...
ユーザーの権限に応じて異なるメニュー ページを表示します。知識ポイントルートガード(事前ガードを使用...
問題を解決するBootstrap は、次の問題を解決する CSS フレームワークです。デバイス間での...
等高レイアウト同じ親コンテナー内の同じ高さの子要素のレイアウトを指します。等高レイアウトの実装の観点...
MySQL で使用される自動インクリメント ID には多くの種類があり、各自動インクリメント ID ...
公式文書には次のように記されている。ルーターを挿入することで、どのコンポーネントでも this.$r...
デフォルトでは、Docker はネットワーク化されていない UNIX ソケット上で実行されます。オプ...
入力ファイルの HTML コントロールを Web ページに追加します。 <input id=&...
準備Windows Server 2008 R2 Enterprise (2.40GH、8GB、64...
0x00 はじめにWordPress は、世界で最も人気のある CMS システムです。PHP と M...
目次1. 小道具親 >>> 子 (Props)子 >>> 親 (...
順序なしリストでは、順序なしリストのシンボルは各リストの前に表示されるドットです。順序付きリスト o...
1. CSS その他のアイコンアイコンを作成するには 3 つの方法があります。写真css (小さな矢...
今日の講義では、HTML におけるタグの表示モードについてお話ししましたが、これはブロックレベルタグ...
要件は次のとおりですテーブル構造、フィールドコメント情報、テーブル名などをエクスポートします。これは...