Mysql如何实现多张⽆关联表查询数据并分页Mysql 多张⽆关联表查询数据并分页
功能需求
在三张没有主外键关联的表中取出⾃⼰想要的数据,并且分页。
数据库表结构
⽔果表:
坚果表:
饮料表:
数据库随便建的,重在⽅法。
主要使⽤UNION ALL 操作符
UNION ALL 操作符⽤于合并两个或多个 SELECT 语句的结果集。
请注意,UNION ALL内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同 ; 另外,UNION ALL结果集中的列名总是等于 UNION ALL中第⼀个 SELECT 语句中的列名。
// 详细sql语句
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为表的别名
最终结果
mysql多表联合查询时出现的分页问题的解决
mysql⼀对多分页问题
部门表:tbl_dept
员⼯表:tbl_emp
数据库sql⽂件
CREATE DATABASE /*!32312 IF NOT EXISTS*/`ssm-crud` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `ssm-crud`;
matlab plot画点/*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,'技术部'),
(2,'业务部'),
(6,'销售部'),
(7,'⼈事部');
/*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,'晓明','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);
分页错误写法(主查询员⼯表) SELECT * FROM tbl_emp e
LEFT JOIN
tbl_dept d
ON d.dept_id = e.d_id
LIMIT 1,10
使⽤⼦查询⽅式解决问题
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
) e
LEFT JOIN tbl_dept d
ON d.dept_id = e.d_id
下⾯代码与之⽆关仅为备份SELECT
ft.id,
ft.partner_id AS partnerId,
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.provice_text AS fpProviceText ,
FROM
(
SELECT
二维傅里叶变换f.id,sizeof和strlen的结果
f.partner_id ,
f.name ,
f.area ,
f.is_default ,
f.is_del,
FROM
bus_freight_template f
LEFT JOIN bus_freight_provice p
ON f.id = p.template_id
de = p.code
de = #[code]phpmyadmin写入shell
GROUP BY f.id
mysql语句多表查询
LIMIT #{startPage},#{pageSize}
) ft
LEFT JOIN bus_freight_provice fp
ON ft.id = fp.template_id
de = fp.code
plate_id IS NOT NULL
de = #[code]
AND fp.is_del = '0'
AND ft.is_del = '0'
order ate_date desc
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论