Christoph Lehmann wrote:
> Hi
> I have 2 tables, say A and B. One field in table A, say A_link, points
> to a field B_link in table B. I need to select entries in A, which
> fulfill two conditions: A_cond1 = XX and B_cond2 = YY.
>
> E.g
> table A table B
>
> A_cond1 A_link B_link B_cond2
> 1 aa aa A
> 1 aa ac B
> 1 ac dd A
> 2 dd ef C
> 2 ef
> 3 aa
> 3 dd
> 3 dd
> 4 ac
> 4 ac
> 4 ef
>
> How can I now define a select statement such as:
>
> SELECT* from A WHERE A_cond1 BETWEEN 1 AND 3 "and corresponding field
> B_cond2 = A"
>
> this would return
>
> A_cond1 A_link
> 1 aa
> 1 aa
> 2 dd
> 3 dd
> 3 dd
>
> many thanks for your kind help
>
> best regards
> Christoph
You need a JOIN:
SELECT *
FROM A
JOIN B ON A.A_link = B.B_link
WHERE A.A_cond1 BETWEEN 1 AND 3
AND B.B_cond2 = 'A';
The ON clause tells how to associate rows in B with rows in A.
See the manual for more <http://dev.mysql.com/doc/refman/5.0/en/join.html>.
Michael