Three-table paging query How do I do that?

Source: Internet
Author: User
Three-table paging query How do I do that?
For example, table A
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 so

Sort by Reverse ID
9 xxx
8 zzz
7 FFF
6 eee
5 DDD
4 AAA
3 CCC
2 BBB
1 AAA



It's best to do it with SQL, because paging is involved, so the way the array is sorted doesn't fit


Reply to discussion (solution)

SELECT * FROM Table aunion Allselect * FROM table bunion Allselect * FROM table corder by id desc;

SQL code?123456select * FROM table aunion Allselect * FROM table bunion Allselect * FROM table corder by id desc;
I'm testing, suggesting this error, for what?
The used SELECT statements has a different number of columns
Is it not possible to have the other fields of the three tables different?

For example, table A
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

Table structure is different, is there any way?

For example, table A
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
A little change is good:
SELECT * FROM Table a UNION ALL SELECT * FROM table b b UNION ALL SELECT * FROM table C c ORDER by c.id Desc;

You don't need a SELECT *, just choose 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.