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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:42:03+00:00 2026-06-17T03:42:03+00:00

What is the proper syntax to combine these two queries? SELECT clicks FROM clicksTable

  • 0

What is the proper syntax to combine these two queries?

SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1

and

SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1

I tried:

SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1
UNION
SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1;

but I get “Incorrect usage of UNION and ORDER BY”.

EDIT
Additionally, I want the result to be returned in a single row. So that I can access the value in php eg

$row['nextclick'] and $row['topclick'] 

From Simon’s suggestion, I should not use UNION because I want to return a single row of data

  • 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-06-17T03:42:04+00:00Added an answer on June 17, 2026 at 3:42 am

    You can’t ORDER BY in your first SELECT and then UNION it.

    Edit
    You can however

    apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

    as in the MySQL UNION documentation

    (SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
    UNION
    (SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
    

    Which then makes your SQL

    (SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1)
    UNION
    (SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1);
    

    Edit 2
    To return in an array

    SELECT (SELECT clicks 
            FROM clicksTable 
            WHERE clicks > 199 
            ORDER BY clicks ASC 
            LIMIT 1) AS NextClick,
           (SELECT clicks 
            FROM clicksTable 
            ORDER BY clicks DESC 
            LIMIT 1) AS TopClick;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the proper syntax here? If (@timestamp < (Select PromoStartTimestamp From @promo)) RAISERROR('Code not
Once I accidentally left out some proper syntax for my media queries at the
I'm having trouble finding out the proper syntax for selecting from a table all
I'm not sure what the proper syntax for using C enums is. I have
Basic question so I feel dumb but..., Whats the proper syntax below in the
What's the proper way to add a literal text value from a field to
Just trying to figure out the proper syntax for making a POST to an
I'm trying to wrap my head around the proper syntax to achieve the following--or
For the life of me I can't figure out the proper syntax for creating
I'm trying to delete a Row, can anyone tell me the proper syntax for

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.