Showing posts with label SQL and number combination search. Show all posts
Showing posts with label SQL and number combination search. Show all posts

Wednesday, October 16, 2013

SQL and number combination search

I have table with 10 number fields (let’s say F1, F2... F10).

Now I have 4 numbers (N1, N2, N3, N4).

I have to find if those 4 numbers appear anywhere in the above table. For example, if F2=N4 & F1=N2 & Fx=N3 & Fy=N1 (any order, any combination).

I was wondering is there quick way to do it via SQL or is it only way to write looooong combination of selects (I am not sure I will be able even complete that in this life time).

Here is SQLFiddel Demo

Below is the sample Query

select * from Tempwhere 'N1' in (F1,F2,F3,F4,F5,F6,F7,F8,F9,F10)  & 'N2' in (F1,F2,F3,F4,F5,F6,F7,F8,F9,F10)  & 'N3' in (F1,F2,F3,F4,F5,F6,F7,F8,F9,F10)  & 'N4' in (F1,F2,F3,F4,F5,F6,F7,F8,F9,F10)