Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL question - find a number from a list

Tags:

sql

oracle

I have a table with 100 numbers in it. Now i have a list of 11 numbers, one of them is not in the table (different for the 100 numbers from it).

When i query after my list of 11 numbers select * from table where number in (' ',' ',' '......) it returns the 10 numbers that are in the table.

Now my question is: how do i find the number from my list which is not in the table? (only with SQL, because right now i am using Excel to do that).

Thank you!

like image 937
Ion Avatar asked Jun 20 '11 08:06

Ion


People also ask

How do I find a specific number in SQL?

Search for "(^|,)234(,$)" with the "rlike" operator for exact number matches.

How do I search for multiple values in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);

How do I find column numbers in SQL?

mysql> SELECT COUNT(*) AS NUMBEROFCOLUMNS FROM INFORMATION_SCHEMA. COLUMNS -> WHERE table_schema = 'business' AND table_name = 'NumberOfColumns'; The output displays the number of columns.


2 Answers

You need to use the values clause:

with yourlist as (
select 1 as i from dual
union all
select 2 as i from dual
...
union all
select 3 as i from dual
)
select yourlist.i
from yourlist
left join yourtable
on yourtable.num = yourlist.i
where yourtable.num is null
like image 163
Denis de Bernardy Avatar answered Oct 02 '22 12:10

Denis de Bernardy


One way would be to put your list of numbers in a second table, then use a left join to find where it doesn't match

SELECT number FROM list
LEFT JOIN table USING(number)
WHERE table.number IS NULL;
like image 35
Paul Dixon Avatar answered Oct 02 '22 12:10

Paul Dixon