Forum Moderators: open

Message Too Old, No Replies

Help with mySQL query

         

JoJoJohnson

11:52 am on Feb 9, 2006 (gmt 0)

10+ Year Member



I have 2 tables (The names below only give an idea of the structure)

Table1

(rough structure)

ID, fk2ID, randomvalue

Table2 (link table rough structure)

fk1ID, fk2ID

I need to make a query which says

SELECT * FROM Table1 WHERE fk2ID = (SELECT fk2ID FROM Table2 WHERE fk1ID='SomeValue')

There is going to be roughly 6 rows for each fk1ID in table 2. I am having trouble making the query select all info from table1 if fk2ID matches any of these 6 values.

Can anyone help me? Im pretty sure its a simple answer, it is just something I have never done before with SQL so am completely clueless to it.

JoJoJohnson

1:15 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



just to expand on my query

i THINK that is what i am trying to achieve

SELECT * FROM Table1 WHERE fk2ID IN (SELECT fk2ID FROM Table2 WHERE fk1ID='SomeValue')

Is my syntax right?

I am running MySQL 4.0.25 and I have read that sub queries are not possible in pre 4.1 versions?

Dijkgraaf

10:05 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about

SELECT Table1.*
FROM Table1, Table2
WHERE Table1.fk2ID = Table2.fk2ID
and Table2.fk1ID='SomeValue'

JoJoJohnson

11:05 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



thanks

I managed to work it out myself in the end with a result similar to that.