简体   繁体   English

别名不在MYSQL中处理IF条件

[英]Alias not working on IF condition in MYSQL

I am writing an mysql query and getting error. 我正在编写一个mysql查询并收到错误。

"Unknown column 'dat' in 'field list'" “'字段列表'中的未知列'dat'”

This error occurred due to use alias in IF condition in mysql. 由于在mysql的IF条件中使用别名而发生此错误。

Here is mysql query : 这是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

Any help would be appriciated.. :) 任何帮助都会被appriciated .. :)

Thanks in advance. 提前致谢。

You can't use alias columns, within your selection of other columns. 在您选择的其他列中,您不能使用别名列。 You'll need to copy the entire part of the query you're aliasing, over and over again. 您需要一遍又一遍地复制查询的整个部分。 ie. 即。 replace all occurrences of dat, after your initial declaration, with (select dDateAdded from bl_transaction_history where nPlayerID=nCustomerID) 在初始声明之后用(从bl_transaction_history中选择dDateAdded,其中nPlayerID = nCustomerID)替换所有出现的dat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM