views:

40

answers:

2

Hello,

Trying to use parameter from external query in subquery FROM clause.

tbl1:
| id | val1 | str1 |
| 1  | 12   | sbc  |
| 2  | 22   | sds  |

tbl2:
| id | val1 | str1 |
| 1  | 1    | cp   |

Trying to write the following query:

select * from
    tbl1 t, (select * from tbl2 where t.id = tbl2.id and tbl2.val1 = 1) tb12;

Expected output:

| id | val1 | str1 | id   | val1 | str1 |
| 1  | 12   | sbc  | 1    | 1    | cp   |
| 2  | 22   | sds  | null | null | null |

Yet it fails with the error:

/* SQL Error (1054): Unknown column 't.id' in 'where clause' */

What am I doing wrong here?

+2  A: 

Is there any reason not to use plain old left join like this:

select * from tbl1 t1 left join tbl2 t2 on t1.id = t2.id;
Nenad
I have updated the question, please note the fixed query to better explain the problem. I would like to do a "conditional left outer join"
Maxim Veksler
+2  A: 
SELECT  *
FROM    tbl1 t
LEFT JOIN
        tbl2 t2
ON      t2.id = t.id
        AND t2.val1 = 1
Quassnoi