我在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;
我首先想要第一个选择show的结果,但是第一个的一些结果在第二个中也很常见,第二个选择将sort_col更改为2.(我在选择中不能使用LIMIT
)
我能做些什么来解决它?
答案 0 :(得分: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'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;