[plug] OK, for all of you SQL gurus out there...

simon simon at plumtek.com
Thu May 26 10:36:07 WST 2005


Excuse if this is oracle-y, but youll get the idea

 SELECT t1.objectId FROM thetable t1 WHERE t1.fieldId = 'bar' AND t1.value =
'1'
 AND t1.objectId = (SELECT t2.objectId FROM thetable t2 WHERE t2.fieldID =
'foo'
 AND t2.value = 'Some text' and t2.objectId = t1.objectId);

Try that - Im a bit thick this morning but it should work.


Andrew Furey (andrew.furey at gmail.com) wrote:
>
> I have a MySQL database containing a table with the following
> structure (CSVd for email):
>
> fieldId, objectId, value
> -------
> foo, 101, "Some text"
> bar, 101, 1
> foo, 102, "Some text"
> bar, 102, 0
> foo, 103, "Another string"
> bar, 103, 1
>
> I'm hoping to construct a _single query_ (the calling object doesn't
> have any processing capabilities as it stands, so no intermediate Perl
> variables etc) whereby I can return "101", since that is the only
> objectId that has "foo" equal to "Some text" AND "bar" equal to 1
> (boolean usage).
>
> If it helps, the values "foo" and "bar" are fixed in this usage so
> they can be hardcoded if needed. The best I've come up with is using a
> subselect (in 4.1, which the database isn't currently so I'd have to
> upgrade it):
>
> SELECT objectId FROM thetable WHERE fieldId = 'bar' AND value = '1'
> AND objectId = (SELECT objectId FROM thetable WHERE fieldID = 'foo'
> AND value = 'Some text');
>
> But that doesn't work as-is because the subselect returns two values
> (101 and 102). I don't mind if the overall query returns multiple
> (distinct) values if it's meant to...
>
> Any ideas? Or is this a pipe dream?
>
> TIA
> Andrew
>
> _______________________________________________
> PLUG discussion list: plug at plug.org.au
> http://www.plug.org.au/mailman/listinfo/plug
> Committee e-mail: committee at plug.linux.org.au
>

--
=================
Simon Scott
simon at plumtek.com
mob: 0409113359
=================






More information about the plug mailing list