各位用户为了找寻关于Mysql 如何实现多张无关联表查询数据并分页的资料费劲了很多周折。这里教程网为您整理了关于Mysql 如何实现多张无关联表查询数据并分页的相关资料,仅供查阅,以下为您介绍关于Mysql 如何实现多张无关联表查询数据并分页的详细内容
mysql 多张无关联表查询数据并分页
功能需求
在三张没有主外键关联的表中取出自己想要的数据,并且分页。
数据库表结构
水果表:
坚果表:
饮料表:
数据库随便建的,重在方法。
主要使用union all 操作符
union all 操作符用于合并两个或多个 select 语句的结果集。
请注意,union all内部的 select 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 select 语句中的列的顺序必须相同 ; 另外,union all结果集中的列名总是等于 union all中第一个 select 语句中的列名。
? 1 2 3 4 5 6 7 8 9 10// 详细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文件
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51create
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);
分页错误写法(主查询员工表)
? 1 2 3 4 5 6 7select
*
from
tbl_emp e
left
join
tbl_dept d
on
d.dept_id = e.d_id
limit 1,10
使用子查询方式解决问题
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19select
*
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
下面代码与之无关 仅为备份
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53select
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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/qq_38159467/article/details/85015559