SQL Alias of joined tables

У меня есть такой запрос:

select a1.name, b1.info 
 from (select name, id, status 
         from table1 a) as a1
right outer join (select id, info 
                    from table2 b) as b1 on (a1.id = b1.id)

Я хочу включить только все, где a1.status = 1 и поскольку Я использую внешнее соединение, я не могу просто добавить ограничение where к table1, потому что вся информация из table2, которую я хочу исключить, все равно будет там, только без имени. Я думал примерно так:

 select z1.name, z1.info 
   from ((select name, id, status 
            from table1 a) as a1
right outer join (select id, info 
                    from table2 b) as b1 on (a1.id = b1.id)) as z1 
  where z1.status = 1

но я не думаю, что это законно.

РЕДАКТИРОВАТЬ: As described below, an outer join actually doesn't make sense for what I'm trying to do. What if, for example, I want all the data from table2 where status!=1 in table1, inclusive of all data where a corresponding ID does not at all exist in table1. Thus I would need an outer join of all data from table2, but still want to exclude those entries where the status=1.

Equivalent to this:

 select z1.name, z1.info 
   from ((select name, id, status 
            from table1 a) as a1
right outer join (select id, info 
                    from table2 b) as b1 on (a1.id = b1.id)) as z1 
  where z1.status != 1
6
задан Lincecum 1 November 2010 в 19:44
поделиться