need query advice

From: Stephan Fiebrandt <bsd(at)kuehlbox(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: need query advice
Date: 2004-10-16 09:08:52
Message-ID: 4170E524.2040101@kuehlbox.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello all,

i've got a psql database, with a table including 6 rows named "zahl1
zahl2 zahl3 zahl4 zahl5 and zahl6" type integer

There are about 10.000 entries in the table. In every entry, the
numbers are uniq to each other. That means, if zahl1=1, then zahl2-6
cannot be also 1. Example:

1, 10, 78, 43, 2, 67 (all 6 numbers are different in the
same line, never a dup)
56, 34, 78, 1, 77, 99 (but repeating in the db tousand times
of course)
1, 2, 3, 4, 5, 6
2, 3, 4, 99, 5, 6
...

$a ... $f are the variables, of the 6 numbers i am searching: The
numbers that are given to search, are also uniq of course, or it would
not make sense.
I need all hits, where at leased 5 of the given 6 numbers are maching in
every entry:

select * from zaehlerzuordnung_snapshots where ((zahl1=$a or zahl2=$a or
zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) and (zahl1=$b or zahl2=$b
or zahl3=$b or zahl4=$b or zahl5=$b or zahl6=$b) and (zahl1=$c or
zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or zahl6=$c) and (zahl1=$d
or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d or zahl6=$d) and
(zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or zahl5=$e or zahl6=$e))

or

((zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or zahl6=$b)
and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or
zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a)
and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or
zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a)
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or
zahl6=$b) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a)
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or
zahl6=$b) and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c
or zahl6=$c) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a)
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or
zahl6=$b) and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c
or zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or
zahl5=$d or zahl6=$d) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f
or zahl5=$f or zahl6=$f)) ;

well.. this query is working. But for sure totaly ineffictive written. I
need the advice of your professionals to more simply that query.
As next problem i have to search if already 4 of the given 6 numbers are
machting the 6 numbers per entrie.
But to write a query like i did above.. wount do the trick i guess. Any
hints? i really need it soon :(

greets,

Yze

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Steven Klassen 2004-10-17 00:49:29 Re: Inserting into table only if the row does not already
Previous Message Tom Lane 2004-10-15 14:21:29 Re: libpq-fe: PQgetvalue() ?