Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:35:09+00:00 2026-05-16T23:35:09+00:00

Let’s assume I have three table – list , a and b table list

  • 0

Let’s assume I have three table – list,a and b

table list
|a_id|--|b_id|--'name'
|4   |--|3   |--|foo|
|1   |--|2   |--|foo|

table a
|id|--|name|--|order|
|1 |--|a_1 |--|1    |
|2 |--|a_2 |--|2    |
.....................
|n |--|a_n |--|n    |

table b
|id|--|name|--|order|
|1 |--|b_1 |--|1    |
|2 |--|b_2 |--|2    |
.....................
|n |--|b_n |--|n    |

a_id in table list is id from a table, and

b_id in table list is id from b table

I need to get the name list from a and b tables ordered by their order, where name = ‘foo’ in list table.

ie. i need to get a_1,a_4 from table a, and b_2,b_3 from b.

I wrote a query like

SELECT
                    `a_1_table`.`name` a_1_name,
                    `b_1_table`.`name` b_1_name
                FROM
                    `list`
                LEFT JOIN 
                    `a_1` AS a_1_table ON ( `a_1_table`.`id` = `list`.`a_id` )
                LEFT JOIN 
                    `b_1` AS b_1_table ON ( `b_1_table`.`id` = `list`.`b_id` )
                WHERE 
                    `list`.`name` = 'foo'

but as I see, in such structure, I can’t write order by .... ie. I can’ but I will not get what I want.

What can I do?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:35:10+00:00Added an answer on May 16, 2026 at 11:35 pm

    Use:

    SELECT x.name
        FROM (SELECT a.name, a.order, 1 AS sort
                FROM LIST l
                JOIN a ON a.id = list.a_id
              WHERE l.name = 'foo'
              UNION ALL
              SELECT b.name, b.order, 2 AS sort
                FROM LIST l
                JOIN b ON b.id = list.b_id
               WHERE l.name = 'foo') x
    ORDER BY x.sort, x.order
    

    UNION ALL is faster than UNION, because it doesn’t remove duplicates (which UNION does).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have a table with a Color column. Color can have various
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let assume we have two activities. A - main activity, that is home launcher
Let's say I have the string: hello world; some random text; foo; How could
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I have an array of random numbers in no particular order. Let's
Let's say, we have and list of objects in variable called articles, each object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.