7

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

3 Answers 3

18

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;
8
  • This only returns one result
    – PutraKg
    Jan 11, 2013 at 15:56
  • @PutraKg see my edit; that should return you what you're looking for Jan 11, 2013 at 16:21
  • Thanks. It's working. But what if I want it to return an array instead of two value? something like in php $row['nextclick'] and $row['topclick']
    – PutraKg
    Jan 11, 2013 at 16:24
  • I'm not sure I follow, I don't know php that well. If what you're asking for is a single row of data (rather than the two this would produce) then don't use the UNION and instead make these nested selects... SELECT * FROM (SELECT clicks.. ), (SELECT clicks...). But that is a different question Jan 11, 2013 at 16:35
  • Simon, I've edited the question. It should not be about UNION in the first place. My apology.
    – PutraKg
    Jan 11, 2013 at 16:41
4
SELECT clicks FROM
    (SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1) A
UNION
SELECT clicks FROM
    (SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1) B;
1

First, do you want a union or a union all?

The problem is the order by in the first part. You can fix this, using subqueries:

(select * from (SELECT clicks FROM clicksTable WHERE clicks > 199 ORDER BY clicks ASC LIMIT 1))
UNION ALL
(select * from (SELECT clicks FROM clicksTable ORDER BY clicks DESC LIMIT 1))

In a union expression, order by is only allowed at the end and it applies to the entire expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.