我想按 IN 值对下面的查询进行排序,例如通过电话 4444,6666,5555, ... 的结果排序
(
select tab_a_user as user, tab_a_phone as phone
from table_a
where tab_a_phone in (4444, 6666, 5555, ...)
)
UNION ALL
(
select tab_b_user as user, tab_b_phone as phone
from table_b
where tab_b_phone in (4444, 6666, 5555, ...)
)
我尝试像下面这样更改我的查询,但似乎缺少右括号
(
select tab_a_user as user, tab_a_phone as phone
from table_a
where tab_a_phone in (4444, 6666, 5555, ...)
order by field(phone,4444, 6666, 5555, ...))
UNION ALL
(
select tab_b_user as user, tab_b_phone as phone
from table_b
where tab_b_phone in (4444, 6666, 5555, ...)
order by field(phone,4444, 6666, 5555, ...))
IN 里面的值只是一个例子,因为在我的程序中,我会放一个变量,所以它会有一堆数据
最佳答案
使用 INSTR
的另一种方法像这样:
select * from (
select tab_a_user as user, tab_a_phone as phone
from table_a
where tab_a_phone in (4444, 6666, 5555)
UNION ALL
select tab_b_user as user, tab_b_phone as phone
from table_b
where tab_b_phone in (4444, 6666, 5555)
)
order by
INSTR('4444, 6666, 5555',phone)
要管理电话号码包含另一个电话号码的案例,您需要添加分隔符:
order by
INSTR('-4444-6666-5555-','-'||phone||'-')
关于sql - 返回带有 union all 的查询结果,其顺序与我的 `IN(…)` 语句中的值相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47069987/