How to query data from multiple unrelated tables and paging in Mysql

How to query data from multiple unrelated tables and paging in Mysql

Mysql multiple unrelated tables query data and paging

Functional requirements

Take out the data you want from three tables that have no primary and foreign key associations, and paginate them.

Database table structure

Fruit table:

insert image description here

Nuts Table:

insert image description here

Drinks List:

insert image description here

The database can be built casually, the method is important.

Mainly use the UNION ALL operator

The UNION ALL operator is used to combine the result sets of two or more SELECT statements.

Note that the SELECT statements inside the UNION ALL must have the same number of columns. Columns must also have similar data types. Also, the order of the columns in each SELECT statement must be the same; in addition, the column names in the UNION ALL result set are always equal to the column names in the first SELECT statement in the UNION ALL.

//Detailed SQL statement select * FROM
(
	(select fid,fname,price,type from fruits)
	UNION ALL
	(select nid,name,price,6 as type from nut)
	UNION ALL
	(select did,dname,price,7 as type from drinks)
	
) as fnd limit 0,10 -----fnd is the alias of the table

Final Result

insert image description here

Solution to the paging problem when querying multiple tables in MySQL

mysql one-to-many paging problem

Department table: tbl_dept

Employee table: tbl_emp

Database sql file

 
CREATE DATABASE /*!32312 IF NOT EXISTS*/`ssm-crud` /*!40100 DEFAULT CHARACTER SET utf8 */;
 
USE `ssm-crud`;
 
/*Table structure for table `tbl_dept` */
 
DROP TABLE IF EXISTS `tbl_dept`;
 
CREATE TABLE `tbl_dept` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
 
/*Data for the table `tbl_dept` */
 
insert into `tbl_dept`(`dept_id`,`dept_name`) values 
(1,'Technical Department'),
(2,'Business Department'),
(6, 'Sales Department'),
(7, 'Personnel Department');
 
/*Table structure for table `tbl_emp` */
 
DROP TABLE IF EXISTS `tbl_emp`;
 
CREATE TABLE `tbl_emp` (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(255) DEFAULT NULL,
  `emp_gender` char(1) DEFAULT NULL,
  `emp_email` varchar(255) DEFAULT NULL,
  `d_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`emp_id`),
  KEY `FK_tbl_emp` (`d_id`),
  CONSTRAINT `FK_tbl_emp` FOREIGN KEY (`d_id`) REFERENCES `tbl_dept` (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
 
/*Data for the table `tbl_emp` */
 
insert into `tbl_emp`(`emp_id`,`emp_name`,`emp_gender`,`emp_email`,`d_id`) values 
(1,'xiaoshen','2',NULL,6),
(4,'Xiaoming','1',NULL,1),
(5,'xiaohong','2',NULL,2),
(6,'xiaohei','2',NULL,6),
(7,'xiaozhang','1',NULL,1),
(8,'xiaogao','1',NULL,1),
(9,'xiaohua','1',NULL,1),
(10,'xiaoyan','2',NULL,1),
(11,'xiaohai','2',NULL,2),
(12,'xiaoqiang','1',NULL,6),
(13,'xiaoqi','2',NULL,7);

Paging error writing method (main query employee table)

SELECT * FROM tbl_emp e
LEFT JOIN 
tbl_dept d
 
ON d.dept_id = e.d_id
 
LIMIT 1,10

Solve the problem using subquery

SELECT
        *
    FROM
     (
       SELECT
       *
        FROM
        tbl_emp e
        LEFT JOIN 
    tbl_dept d
        ON d.dept_id = e.d_id
        
        GROUP BY e.d_id
        LIMIT 1,10
 
     )
 
      LEFT JOIN tbl_dept d
        ON d.dept_id = e.d_id

The following code is irrelevant and is only for backup

SELECT
        ft.id,
        ft.partner_id AS partnerId,
        ft.code ,
        ft.end_update_date AS endUpdateDate,
        ft.name ,
        ft.type ,
        ft.area,
        ft.is_default AS isDefault,
        fp.id fpId,
        fp.shop_id AS fpShopId ,
        fp.provice_id AS fpProviceId ,
        fp.provice_name AS fpProviceName ,
        fp.start_num AS fpStartNum ,
        fp.start_fee AS fpStartFee ,
        fp.increase_num AS fpIncreaseNum ,
        fp.increase_fee AS fpIncreaseFee ,
        fp.code AS fpCode ,
        fp.provice_text AS fpProviceText ,
        fp.template_id AS fpTemplateId
    FROM
     (
       SELECT
        f.id,
        f.partner_id ,
        f.code ,
        f.end_update_date ,
        f.name ,
        f.type ,
        f.area ,
        f.is_default ,
        f.is_del,
        f.create_date
        FROM
        bus_freight_template f
        LEFT JOIN bus_freight_provice p
        ON f.id = p.template_id
        WHERE f.code = p.code
        AND f.code = #[code]
        GROUP BY f.id
        LIMIT #{startPage},#{pageSize}
 
     ) ft
 
      LEFT JOIN bus_freight_provice fp
        ON ft.id = fp.template_id
    WHERE ft.code = fp.code
      AND fp.template_id IS NOT NULL
      AND ft.code = #[code]
      AND fp.is_del = '0'
      AND ft.is_del = '0'
 
      order by ft.create_date desc

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation of MySQL large page query optimization for millions of data
  • How to implement paging query in MySQL
  • MySQL paging query optimization techniques
  • MySQL query sorting and paging related
  • MySQL optimization tutorial: large paging query
  • How to implement paging query using MySQL

<<:  The content of the commonly used input text box is automatically vertically centered and the default prompt text is empty when clicked

>>:  How to add automatic completion commands for docker and kubectl on Mac

Recommend

MySQL 5.6.22 installation and configuration method graphic tutorial

This tutorial shares the specific code of MySQL5....

A brief discussion on the lock range of MySQL next-key lock

Preface One day, I was suddenly asked about MySQL...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

Let's talk about Vue's mixin and inheritance in detail

Table of contents Preface Mixin Mixin Note (dupli...

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

MySQL million-level data paging query optimization solution

When there are tens of thousands of records in th...

Vue implements verification code countdown button

This article example shares the specific code of ...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

JS realizes the front-end paging effect

This article example shares the specific code of ...

Correct way to load fonts in Vue.js

Table of contents Declare fonts with font-face co...

How to run a project with docker

1. Enter the directory where your project war is ...