[Zope-DB] SQL-subselect question

Charlie Clark charlie@begeistert.org
Wed, 20 Nov 2002 17:17:14 +0100


Dear list,

can anyone point me in the right direction?
I've got a table which has entries a bit like
p_key=09f_key=09ext_id1=09ext_id2=09ext_id3=09ext_id4
1=09=091=09=09=091
2=09=091=09=09=09=09=091
3=09=091=09=09=09=09=09=09=092
4=09=091=09=09=09=09=09=09=09=09=091
5=09=091=09=09=09=09=092
6=09=092=09=09=091
7=09=092=09=09=09=09=09=09=091
....

I need to be able to do something like
SELECT * from mytable, anothertable
WHERE
anothertable.value =3D 'something'
AND
f_key =3D anothertable.f_key
AND
ext_id1 =3D 1
AND 
ext_id2 =3D 1

this currently doesn't work as there is only one ext_id per row

I think subselects should work but I haven't been able to write them 
properly

SELECT * from anothertable
WHERE
anothertable.value =3D 'something'
AND EXISTS
=09(SELECT * from mytable WHERE ext_id1 =3D 1 AND mytable.f_key =3D 
anothertable.f_key)
AND EXISTS
=09(SELECT * from mytable WHERE ext_id2 =3D 1 AND mytable.f_key =3D 
anothertable.f_key)

this works but is incredibly slow (it's looping I think)

Any help is much appreciated.

Charlie