热搜:NVER node 开发 php

三个表分页查询请问如何做到?

2024-09-07 20:10:01
三个表分页查询请问如何做到?

三个表分页查询请问如何做到?
例如表A
id name
1   aaa
2   bbb
3   ccc
表B
5 ddd
6 eee
7 fff

表C
4 aaa
8 zzz
9 xxx

如何能查询结果是这样

以ID倒序排序
9 xxx
8 zzz
7 fff
6 eee
5 ddd
4 aaa
3 ccc
2 bbb
1 aaa



最好用SQL完成,因为涉及到分页,所以数组排序的方式不适合


回复讨论(解决方案)

select * from 表Aunion allselect * from 表Bunion allselect * from 表Corder by id desc;

SQL code?123456select * from 表Aunion allselect * from 表Bunion allselect * from 表Corder by id desc;
我测试 ,提示这个错误,是为什么呢
The used SELECT statements have a different number of columns
三个表的 其他字段不同也不可以吗?

例如表A
id a_name
1   aaa
2   bbb
3   ccc
表B
id b_name
5 ddd
6 eee
7 fff

表C
id c_name
4 aaa
8 zzz
9 xxx

表结构不同,有什么办法吗

例如表A
id a_name
1   aaa
2   bbb
3   ccc
表B
id b_name
5 ddd
6 eee
7 fff

表C
id c_name
4 aaa
8 zzz
9 xxx
稍加改动就好:
select * from 表A a union all select * from 表B b union all select * from 表C c order by c.id desc;

你不要select * ,直接选择你要的字段呀。

UNOIN ALL