列名sql isnull()子句在哪里

时间:2011-06-15 08:39:27

标签: sql sql-server where-clause

我无法在IdCell列上创建where子句,但我可以执行“order by”。

SQL服务器说列名称错误

    select 
    *, (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) as IdCell
    from Table_Tickets 
    left join Table_Postes on Table_Postes.IdPoste = Table_Tickets.IdPoste
    left join Table_Adresses on Table_Adresses.IdAdresse = Table_Postes.IdAdressePhysique
    left join Table_Clients on Table_Clients.IdClient = Table_Tickets.IdClient
    where idtypeticket=2 and 
    isnull(bcloture,0)=0 and 
    IdCell =4 --problem here
    order by IdCell

为什么我不能在IdCell上找到一个位置?

谢谢

4 个答案:

答案 0 :(得分:2)

尽管SQL(该语言)强制您按SELECT-FROM-WHERE-ORDER BY的顺序构建查询,但它(有效地)以FROM-WHERE-SELECT-ORDER BY的顺序执行。这就是为什么您可以在AS子句中使用IdCell子句(“列别名”)ORDER BY但不在WHERE子句中使用IdCell不是WHERE在{{1}}子句的范围内,因为它只在以后出现。

答案 1 :(得分:0)

您不能以这种方式使用别名列,您需要执行以下操作:

and (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) = 4

答案 2 :(得分:0)

   select 
    *, (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) as IdCell
    from Table_Tickets 
    left join Table_Postes on Table_Postes.IdPoste = Table_Tickets.IdPoste
    left join Table_Adresses on Table_Adresses.IdAdresse = Table_Postes.IdAdressePhysique
    left join Table_Clients on Table_Clients.IdClient = Table_Tickets.IdClient
    where idtypeticket=2 and 
    isnull(bcloture,0)=0 and 
    (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) =4 
    order by IdCell

答案 3 :(得分:0)

select 
    *, (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) as IdCell
    from Table_Tickets 
    left join Table_Postes on Table_Postes.IdPoste = Table_Tickets.IdPoste
    left join Table_Adresses on Table_Adresses.IdAdresse = Table_Postes.IdAdressePhysique
    left join Table_Clients on Table_Clients.IdClient = Table_Tickets.IdClient
    where idtypeticket=2 and 
    bcloture is null and
    (ISNULL(Table_Adresses.IdCellule, Table_Clients.IdCellule)) = 4 
    order by IdCell