我无法在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上找到一个位置?
谢谢
答案 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