Hi,

I have 2 tables, "X" and "Y".

table X          table Y
+----+-------+   +----+-------+
| id | title |   | id | assoc |
+----+-------+   +----+-------+
|  1 | one   |   |  1 |     3 |
|  2 | two   |   |  2 |     3 |
|  3 | three |   |  2 |     4 |
|  4 | four  |   +----+-------+
|  5 | five  |
+----+-------+

I wish to retrieve the X.id and X.title where X.id=Z
and also the X.id and X.title where (X.id=Y.assoc) when (Y.id=X.id)
but I'm trouble trying to express this as a single SQL statement.
Could someone help me out with this, or at least give me a clue as to how to
think this one through?

In case I haven't explained it very well, I'll describe exactly what I'm
doing.
Z is a product number, and I want to retrieve the (table X) data for product
Z and also the (table X) data for any other products associated with it,
represented by table Y.

I know I could do it using 2 select statements, firstly just selecting
X.id, X.title, Y.id, Y.assoc where X.id=Z
(I'm using perl DBI)
and then within the perl program create another SQL statement selecting
X.id, X.title WHERE Y.assoc IS NOT NULL in the results from the first
select.
However, I would like to do this with one SELECT if it's possible, for
efficiency.

Thanks,
Carl







---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to