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 5997985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:20:29+00:00 2026-05-23T00:20:29+00:00

My table strucutre is as follows | id | cmp | empid | empname

  • 0

My table strucutre is as follows

| id   | cmp      | empid | empname | ttm  
+------+----------+-------+---------+------
|    2 | xyz      | 12    | swap    |    2 
|    2 | xyz      | 12    | sag     |    3
|    2 | xyz      | 14    | azr     |    1 
|    3 | pqr      | 2     | ron     |    2 
|    3 | pqr      | 22    | rah     |    1 
|    3 | pqr      | 32    | pra     |    5 

I have done query on that like as follows

(select * from test.companies where id = '2' order by ttm desc limit 2)
union
(select * from test.companies where id = '3' order by ttm desc limit 2);

it will give output as follows

| id   | cmp      | empid | empname | ttm  
+------+----------+-------+---------+------
|    2 | wissen   | 12    | sag     |    3
|    2 | wissen   | 12    | swap    |    2 
|    3 | prolinkd | 32    | pra     |    5
|    3 | prolinkd | 2     | ron     |    2 

but i want output as follows

| id   | cmp      | empid | empname | ttm  
+------+----------+-------+---------+------
|    3 | prolinkd | 32    | pra     |    5
|    3 | prolinkd | 2     | ron     |    2
|    2 | wissen   | 12    | sag     |    3 
|    2 | wissen   | 12    | swap    |    2 

means which company has maximum ttm it will be shown first

If some one knows plz reply

Thanks

  • 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-23T00:20:30+00:00Added an answer on May 23, 2026 at 12:20 am

    Rather ugly looking, and ugly sounding, but it should do what you want. It retrieves the “maxttm” for each company with each record, then uses that in sorting to allow you to assign priority to companies based on their highest ttm.

    SELECT * FROM (
        (SELECT *,
            (SELECT ttm FROM test.companies ic WHERE ic.id = oc.id ORDER BY ttm DESC LIMIT 1) AS maxttm
            FROM test.companies oc WHERE id = '2' ORDER BY ttm DESC LIMIT 2)
        UNION
        (SELECT *,
            (SELECT ttm FROM test.companies ic WHERE ic.id = oc.id ORDER BY ttm DESC LIMIT 1) AS maxttm
            FROM test.companies oc WHERE id = '3' ORDER BY ttm DESC LIMIT 2)
    ) AS myunion ORDER BY maxttm, id;
    

    PS. thanks to inti whose query I shamelessly based this on.

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

Sidebar

Related Questions

hopefully somebody can help The table structure is as follows: tblCompany: compID compName tblOffice:
My table structure is as follows: Person 1-M PesonAddress Person 1-M PesonPhone Person 1-M
The current structure is as follows: Table RowType: RowTypeID Table RowSubType: RowSubTypeID FK_RowTypeID Table
My table structure is as follows: mid | tid | data | 1 |
I have table structure as follows id productid ip hittime --------------------------------------------------------------------------- 1 5 1.1.1.1
I have a table structure as follows and I want to select all active
I have the following table structure as follows: (please ignore the blackbox. was formatting
I have a table structure as follows. Now I need to sort these nested
I've got the following problem. My table ( geo_table ) structure is as follows:
I have database table whose structure is as follows: id int name varchar(20) dob

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.