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

  • Home
  • SEARCH
  • 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 3800752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:56:32+00:00 2026-05-19T13:56:32+00:00

I have a select statement returning 5 columns: select col1,col2,col3,col4,col5 from table1; col1 col2

  • 0

I have a select statement returning 5 columns:

select col1,col2,col3,col4,col5 from table1;

col1 col2 col3 col4 col5
 9     A    B   C    D
 8     E    F   G    H 

I have another select statement from table2 which returns col1 alone;

col1
8
9

Based on the two select queries, is there a way to write a single select query to return the result as:

 col1 col2 col3 col4 col5
    8     E    F   G    H 
    9     A    B   C    D

ie. basically sort the output of I query based on col1 from II query. (this is in Mysql)

PS:II table column1 is used to for sorting & that is coming from table 2. Table2’s col1 is not static, its changing for every user action & based on a call i will get col1 of table 2 & need to sort with table1’s output.

  • 1 1 Answer
  • 0 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-19T13:56:33+00:00Added an answer on May 19, 2026 at 1:56 pm

    For this to work, you seriously need a sort column on table2. Just having the IDs in table2 is not enough. You can have the records 7,8,9, then delete 8 and add it back. But no, that doesn’t order it as 7,9,8. Maybe temporarily if there is no primary key on the table, but when the table gets large, even that “implicit” order is lost.

    So, assuming you have such a sort column

    Table2
    Sort, Col1
    1, 9
    2, 8
    

    Your query becomes

    SELECT a.*
    FROM table1 a
    INNER JOIN table2 b ON a.col1 = b.col1
    ORDER BY b.sort ASC
    

    If you still want to rely on MySQL undocumented features or the way it currently works, then you can try this.

    # test tables
    create table table1 (col1 int, col2 int, col3 int);
    insert table1 select 8, 1,2;  # in this order
    insert table1 select 9, 3,4;
    
    create table table2 (col1 int);
    insert table2 select 9;  # in this order
    insert table2 select 8;
    
    # select
    SELECT a.*
    FROM table1 a
    INNER JOIN table2 b ON a.col1 = b.col1
    
    ----output----
    col1  col2  col3
    9     3     4
    8     1     2
    

    This works at least for small tables, only because size(table2) < size(table1) so it collects in that order, preserving the filesort on table2.col1.

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

Sidebar

Related Questions

I have the following statement SELECT id, descr from mytable which returns 1, 'Test1'
I have a select statement with calculated columns and I would like to use
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
I have two sql queries select * from table1 ORDER BY column1 Select top
Suppose I have a SELECT statement that returns some set of results. Is there
I am trying to find a way to have a SELECT statement return the
I have a relatively simple select statement in a VB6 program that I have

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.