简体   繁体   中英

Select rows and keep order of text in where clause

I have a lot of text data that I must use as a key to return other data from the database. The problem is that some of those keys dont exist. They query I am looking for should return empty rows for them

select column1, column2,column3 from mobilenumbers where mobile_number in ('123456789','123456798');

So far the only way I have found is to use a right outer join, however that only works on two tables, I have one table and text data as the keys.

And before anyone asks...I cannot create a table to put this data in.

If it helps, this is an oracle 10g database.

you can do this.

first, get a public varrray of your choice ..theres usually quite a few about.

--pick a public VARRAY.
select owner, type_name from all_Coll_types where coll_type = 'VARYING ARRAY' 
and elem_type_name = 'VARCHAR2' ;

then select keeping the order of your IN list:

with numbers as (select /*+ cardinality(t, 10) */ rownum r, t.column_value pnumber
  from table(sys.dbmsoutput_linesarray('030390483', '089847547', '0347839874', '020983438')) t
)
select n.pnumber, m.mobile_number
  from numbers n
       left outer join mobilenumbers m
                    on n.pnumber = m.mobile_number
 order by n.r;

eg: http://sqlfiddle.com/#!4/9da70/1

This is rather ugly but you can try:

SELECT column1, column2, column3
FROM (SELECT 1 FROM DUAL) x
LEFT OUTER JOIN mobilenumbers ON mobile_number = '123456789'
UNION ALL
SELECT column1, column2, column3
FROM (SELECT 1 FROM DUAL) x
LEFT OUTER JOIN mobilenumbers ON mobile_number = '123456798'

SQL Fiddle example

Or, you can use this trick, but it may not maintain the order you specify the numbers in:

SELECT column1, column2, column3
FROM TABLE(sys.odcivarchar2list('123456789','123456798')) x
LEFT OUTER JOIN mobilenumbers m ON x.COLUMN_VALUE = m.mobile_number

SQL Fiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM