Example of how to retrieve the latest data using MySQL multi-table association one-to-many query

Example of how to retrieve the latest data using MySQL multi-table association one-to-many query

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 data

Problems encountered

Multiple tables are associated with one-to-many queries to obtain the latest data, and the data is repeated

Due to historical reasons, the table structure design is unreasonable; the product told me that I need to export customer information data, the customer's industry , and tax nature data; but these two fields are in the order table, and customers are required to fill them in every time they place an order; it can be seen that customer data and order data are a one-to-many relationship; then the question is, which data in the order should I take as the basis? After consultation, it was agreed that the latest data shall prevail;

Data test initialization SQL script

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');

  • SQL statements written as required:
UPDATE order_info SET create_time = NOW();
  • Try to solve the problem
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;

Data duplication? No problem. Can’t we just add a GROUP BY to solve the problem? How can I be so clever, hahaha! ! ! But when I finished executing the SQL, I was confused again. The industry and tax nature in the query results were still not the latest. It seemed that I thought too much, so I should solve the problem honestly. . .

  • Find duplicate data
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;
  • The execution results are as follows:
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;

Hey, finally solved it. . .

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:
  • MySQL implements multi-table association statistics (subquery statistics) example
  • A brief discussion on the implementation of multi-table unrelated query in MySQL
  • MySQL detailed explanation of multi-table association query

<<:  js realizes two-way data binding (accessor monitoring)

>>:  Docker+K8S cluster environment construction and distributed application deployment

Recommend

How to hide and remove scroll bars in HTML

1. HTML tags with attributes XML/HTML CodeCopy co...

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

JavaScript implements simple calculator function

This article example shares the specific code of ...

JavaScript to achieve skin effect (change background)

This article shares the specific code of JavaScri...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

JavaScript immediate execution function usage analysis

We know that in general, a function must be calle...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

Seven solutions for classic distributed transactions between MySQL and Golan

Table of contents 1. Basic theory 1.1 Transaction...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

Analysis of the Neglected DOCTYPE Description

doctype is one of them: <!DOCTYPE HTML PUBLIC &...