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`; /*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.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
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。