This article uses an example to describe how to retrieve the latest data using a one-to-many query with multiple tables in MySQL. Share with you for your reference, the details are as follows: MySQL multi-table association one-to-many query to get the latest dataProblems encounteredMultiple tables are associated with one-to-many queries to obtain the latest data, and the data is repeated
DROP TABLE IF EXISTS `customer`; CREATE TABLE `customer` ( `id` BIGINT NOT NULL COMMENT 'Customer ID', `real_name` VARCHAR(20) NOT NULL COMMENT 'Customer name', `create_time` DATETIME NOT NULL COMMENT 'Creation time', PRIMARY KEY (`id`) )ENGINE=INNODB DEFAULT CHARSET = UTF8 COMMENT 'Customer information table'; -- DATA FOR TABLE customer INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7717194510959685632', '张三', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7718605481599623168', '李四', '2019-01-23 16:23:05'); INSERT INTO `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'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722233303626055680', 'Baby', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722233895811448832', 'Xiaobao', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722234507982700544', '大宝', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722234927631204352', '二宝', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722235550724423680', '小贱', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722235921488314368', 'Xiao Ming', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722238233975881728', '小黑', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722246644138409984', 'Xiaohong', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722318634321346560', '阿狗', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722318674321346586', '阿娇', '2019-01-23 16:23:05'); INSERT INTO `demo`.`customer` (`id`, `real_name`, `create_time`) VALUES ('7722318974421546780', '阿猫', '2019-01-23 16:23:05'); DROP TABLE IF EXISTS `order_info`; CREATE TABLE `order_info` ( `id` BIGINT NOT NULL COMMENT 'Order ID', `industry` VARCHAR(255) DEFAULT NULL COMMENT 'Industry', `nature_tax` VARCHAR(255) DEFAULT NULL COMMENT 'Tax nature', `customer_id` VARCHAR(20) NOT NULL COMMENT 'Customer ID', `create_time` DATETIME NOT NULL COMMENT 'Creation time', PRIMARY KEY (`id`) )ENGINE=INNODB DEFAULT CHARSET = UTF8 COMMENT 'Order information table'; -- DATA FOR TABLE order_info INSERT INTO `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'); INSERT INTO `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'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7700167995646615552', 'High-tech', 'General taxpayer', '7718605481599623168', '2019-01-23 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7700167995646615553', 'Commerce', 'General Taxpayer', '7718605481599623168', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7700193633216569344', 'Commerce', 'General Taxpayer', '7720804666226278400', '2019-01-23 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7700193633216569345', 'High-tech', 'General taxpayer', '7720804666226278400', '2019-01-23 17:09:53'); INSERT INTO `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'); INSERT INTO `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'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7703053372673171456', 'High-tech', 'Small scale', '7722233303626055680', '2019-01-23 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7703053372673171457', 'High-tech', 'Small scale', '7722233303626055680', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709742385262698496', 'Service Category', 'General Taxpayer', '7722233895811448832', '2019-01-23 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709742385262698498', 'Service Category', 'General Taxpayer', '7722233895811448832', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745055683780608', 'High-tech', 'Small scale', '7722234507982700544', '2019-01-23 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745055683780609', 'Import and Export', 'Small Scale', '7722234507982700544', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745249439653888', 'Culture and Sports', 'General Taxpayer', '7722234927631204352', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745249439653889', 'High-tech', 'General taxpayer', '7722234927631204352', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745453266051072', 'High-tech', 'Small scale', '7722235550724423680', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745453266051073', 'Culture and Sports', 'Small Scale', '7722235550724423680', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745539848413184', 'Technology', 'General Taxpayer', '7722235921488314368', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745539848413185', 'High-tech', 'General taxpayer', '7722235921488314368', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745652603887616', 'High-tech', 'General taxpayer', '7722238233975881728', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745652603887617', 'Technology', 'General Taxpayer', '7722238233975881728', '2019-01-23 17:09:53'); INSERT INTO `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'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745755528568833', 'Education Consulting', 'Small Scale', '7722246644138409984', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709745892539047936', 'Education Consulting', 'General Taxpayer', '7722318634321346560', '2019-01-24 16:54:25'); INSERT INTO `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'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709746000127139840', 'Production', 'Small Scale', '7722318674321346586', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709746000127139841', 'Agriculture', 'General Taxpayer', '7722318674321346586', '2019-01-23 17:09:53'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709746447445467136', 'Agriculture', 'General taxpayer', '7722318974421546780', '2019-01-24 16:54:25'); INSERT INTO `demo`.`order_info` (`id`, `industry`, `nature_tax`, `customer_id`, `create_time`) VALUES ('7709746447445467137', 'Production', 'Small Scale', '7722318974421546780', '2019-01-23 17:09:53');
UPDATE order_info SET create_time = NOW();
SELECT cr.id, cr.real_name, oi.industry, oi.nature_tax FROM customer AS cr LEFT JOIN ( SELECT a.industry, a.nature_tax, a.customer_id, a.create_time FROM order_info AS a LEFT JOIN ( 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 WHERE a.create_time = b.create_time ) AS oi ON oi.customer_id = cr.id GROUP BY cr.id;
SELECT cr.id, cr.real_name, oi.industry, oi.nature_tax FROM customer AS cr LEFT JOIN ( SELECT a.industry, a.nature_tax, a.customer_id, a.create_time FROM order_info AS a LEFT JOIN ( 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 WHERE a.create_time = b.create_time ) AS oi ON oi.customer_id = cr.id GROUP BY cr.id HAVING COUNT(cr.id) >= 2;
SELECT cr.id, cr.real_name, oi.industry, oi.nature_tax FROM customer AS cr LEFT JOIN ( SELECT a.industry, a.nature_tax, a.customer_id, a.create_time FROM order_info AS a LEFT JOIN ( SELECT MAX(id) AS id, customer_id FROM order_info GROUP BY customer_id ) AS b ON a.customer_id = b.customer_id WHERE a.id = b.id ) AS oi ON oi.customer_id = cr.id;
Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: js realizes two-way data binding (accessor monitoring)
>>: Docker+K8S cluster environment construction and distributed application deployment
Table of contents Login business process Login fu...
Table of contents A JSON is built on two structur...
1. Installation 1. Download MySQL Download addres...
What does linux cd mean? In Linux, cd means chang...
Implementation of time comparison in MySql unix_t...
In many cases, in order to beautify the form, the ...
Table of contents 1. Preprocessing 2. Compilation...
Table of contents Technology Stack Effect analyze...
Table of contents Why do we need a material libra...
This article mainly introduces the principle and ...
1. Create a test table CREATE TABLE `testsign` ( ...
This article mainly introduces the sample code of...
Origin: A few days ago, a tester sent a requireme...
Web Services are concerned with application-to-ap...
Table of contents 1. What is lazy loading of rout...