-1

我在 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;

我首先想要第一个选择显示的结果,但是第一个的一些结果在第二个中也很常见,第二个选择将它们的 sort_col 更改为 2。(我不能LIMIT在我的选择中使用)我能做些什么来解决它?

4

1 回答 1

2

使用子查询:

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

编辑(在OP 的编辑之后)

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;
于 2013-07-24T09:48:23.077 回答