Mook Koom
Mook Koom

Reputation: 55

How to Order the Results of a Union Query By Which Of The Two Queries Comes From?

basically I want this:

    SELECT column FROM Table Where column LIKE "%interesting%"
    UNION
    SELECT column FROM Table 
    ORDER BY "the interesting results first, the fillers second." 

I do this in case the interesting results are few, so that I fill them with random ones.

Upvotes: 0

Views: 31

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

You simply want an ORDER BY:

SELECT column
FROM Table 
ORDER BY (CASE WHEN column LIKE '%interesting%' THEN 1 ELSE 2 END);

Upvotes: 3

Related Questions