-1

I have a stored procedure like below in MYSQL:

    (SELECT 1 AS sort_col, col_a, col_b FROM t1 WHERE col_b LIKE %some% AND col_b LIKE %thing%)
    UNION
    (SELECT 2, col_a, col_b FROM t1 WHERE col_b LIKE %some% OR col_b LIKE %thing%)
 ORDER BY sort_col, col_a;

I want the result of first select show at first, but some results of the first are common in second too, and second select changed their sort_col to 2.(I can't use LIMIT in my selects) what can I do for solve it?

Fatemeh Gharri
  • 293
  • 1
  • 5
  • 18

1 Answers1

2

Use a sub-query:

SELECT * FROM
(
   (SELECT 1 AS sort_col, col1a, col1b FROM t1)
   UNION
   (SELECT 2, col2a, col2b FROM t2)
) tbl 
ORDER BY sort_col, col1a;

Edit (after OP's edit)

SELECT * FROM
(
  (SELECT 1 AS sort_col, col_a, col_b 
     FROM t1 
    WHERE col_b LIKE %some% 
      AND col_b LIKE %thing%)
  UNION
  (SELECT 2, col_a, col_b 
     FROM t1 
    WHERE col_b LIKE %some% 
       OR col_b LIKE %thing%)
) tbl
ORDER BY sort_col, col_a;
Community
  • 1
  • 1
Himanshu Jansari
  • 30,115
  • 28
  • 106
  • 129