How can I query three tables by page?

Source: Internet
Author: User
How can I query three tables by page? How can I query three tables by page?
For example, Table
Id name
1 aaa
2 bbb
3 ccc
Table B
5 ddd
6 eee
7 fff

Table C
4 aaa
8 zzz
9 xxx

How can I query the results?

Sort by ID in reverse order
9 xxx
8 zzz
7 fff
6 eee
5 ddd
4 aaa
3 ccc
2 bbb
1 aaa



It is best to use SQL. because paging is involved, the array sorting method is not suitable.


Reply to discussion (solution)

Select * from table Aunion allselect * from table Bunion allselect * from table Corder by id desc;

SQL code? 123456 select * from table Aunion allselect * from table Bunion allselect * from table Corder by id desc;
Why is this error prompted when I test it?
The used SELECT statements have a different number of columns
Are other fields in the three tables different?

For example, Table
Id a_name
1 aaa
2 bbb
3 ccc
Table B
Id B _name
5 ddd
6 eee
7 fff

Table C
Id c_name
4 aaa
8 zzz
9 xxx

What can I do if the table structure is different?

For example, Table
Id a_name
1 aaa
2 bbb
3 ccc
Table B
Id B _name
5 ddd
6 eee
7 fff

Table C
Id c_name
4 aaa
8 zzz
9 xxx
Slightly changed:
Select * from table A a union all select * from Table B union all select * from Table C c order by c. id desc;

Do not select *. select the field you want.

UNOIN ALL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.