【问题标题】:Alias not working on IF condition in MYSQL别名不适用于 MYSQL 中的 IF 条件
【发布时间】:2012-05-05 14:13:35
【问题描述】:

我正在编写一个 mysql 查询并收到错误。

“‘字段列表’中的未知列‘dat’”

由于在mysql的IF条件中使用别名而发生此错误。

这里是mysql查询:

SELECT 
    nCustomerID,
    dDateRegistered,
    (select count(nPlayerID) from credit_logs 
        where nPlayerID=nCustomerID) as total_clog,
    (select count(nPlayerID) FROM bl_transaction_history
        where nPlayerID=nCustomerID) as total_tran,
    (select count(nCustomerID) from customer_freeplays
        where nCustomerID=nCustomerID) as total_free,
    (select dDateAdded from bl_transaction_history
        where nPlayerID=nCustomerID) as dat,
    (select DATEDIFF(now(),dat)/30 ) as date_differece1,
    (select DATEDIFF(now(),dDateRegistered)/30 ) as date_difference2,
    IF (dat IS NOT NULL,(select DATEDIFF(now(),dat)/30 ),
        (select DATEDIFF(now(),dDateRegistered)/30 )) as date_difference
FROM bl_customers
WHERE nAccountStatus=1 
  and bDeleted=0 
having total_clog>0 
    or total_tran>0 
    or total_free>0

任何帮助都会得到满足.. :)

提前致谢。

【问题讨论】:

  • where nCustomerID=nCustomerID 可能需要更正。

标签: mysql alias mysql-error-1064


【解决方案1】:

您不能在选择的其他列中使用别名列。您需要一遍又一遍地复制您正在使用别名的查询的整个部分。 IE。在初始声明之后,将所有出现的 dat 替换为 (select dDateAdded from bl_transaction_history where nPlayerID=nCustomerID)

【讨论】:

猜你喜欢
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多