pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Fiebrandt <bsd@kuehlbox.de>
To: pgsql-sql@postgresql.org
Subject: need query advice
Date: Sat, 16 Oct 2004 11:08:52 +0200
Message-ID: <4170E524.2040101@kuehlbox.de> (raw)

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



view thread (2+ messages)  latest in thread

Message-ID: <4170E524.2040101@kuehlbox.de>
Permalink:  ../4170E524.2040101@kuehlbox.de/
Also on:    postgresql.org/message-id/4170E524.2040101@kuehlbox.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bsd@kuehlbox.de
  Subject: Re: need query advice
  In-Reply-To: <4170E524.2040101@kuehlbox.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox