返回带有union的查询结果,其顺序与我的`IN(...)`语句中的值相同

时间:2017-11-02 07:41:05

标签: sql database oracle

我想通过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里面的值只是一个例子,因为在我的程序中,我会放一个变量,所以它会有一堆数据

4 个答案:

答案 0 :(得分:2)

使用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||'-')

答案 1 :(得分:1)

您需要使用case语句提供订单映射,然后您可以在表达式上使用order by(在UNION之后,您需要使用嵌套查询):

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)

) as X
order by
( case
  when phone = 4444 then 1
  when phone = 6666 then 2
  when phone = 5555 then 3
  end
)

或CTE语法:

with X as (

  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 *
from X
order by
( case
  when phone = 4444 then 1
  when phone = 6666 then 2
  when phone = 5555 then 3
  end
)

如果您有桌子强制您的订单:

order_map.phone order_map.sort_value 
4444            1
6666            2
5555            3


with X as (

  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 *
from X
    join order_map on
        X.phone = order_map.phone
order by
   order_map.sort_value 

答案 2 :(得分:1)

它不能很好地扩展,因为它涉及硬编码,但使用ORDER BY子句和CASE,你可以做到:

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
   CASE WHEN phone = 4444 THEN 1
        WHEN phone = 6666 THEN 2
        WHEN phonb = 5555 THEN 3 
   END

答案 3 :(得分:0)

Oracle安装程序

CREATE TABLE table_a ( tab_a_user, tab_a_phone ) AS
SELECT 'a1', 6666 FROM DUAL UNION ALL
SELECT 'a2', 4444 FROM DUAL;

CREATE TABLE table_b ( tab_b_user, tab_b_phone ) AS
SELECT 'b1', 3333 FROM DUAL UNION ALL
SELECT 'b2', 5555 FROM DUAL;

<强>查询

WITH orders ( idx, value ) AS (
  SELECT ROWNUM, COLUMN_VALUE
  FROM   TABLE( SYS.ODCINUMBERLIST( 4444, 6666, 5555 ) )
)
SELECT usr, phone
FROM   (
  SELECT idx,
         tab_a_user AS usr,
         tab_a_phone AS phone
  FROM   table_a a
         INNER JOIN orders o
         ON ( a.tab_a_phone = o.value )
  UNION ALL
  SELECT idx,
         tab_b_user,
         tab_b_phone
  FROM   table_b b
         INNER JOIN orders o
         ON ( b.tab_b_phone = o.value )
)
ORDER BY idx;

<强>输出

USR PHONE
--- -----
a2   4444
a1   6666
b2   5555