我知道我可以做到:
IF( 2 = 2 AND 0 = 0, 1, 0 ) AS some_result,
(当然输出1)
但是我在查询中有一些内部选择,例如:
(select ... ) as innerA,
(select ... ) as innerB,
(select ... ) as innerC,
然后我想做类似的事情:
IF( innerA, innerB, innerC ) AS my_result,
(如果innerA则使用innerB,否则使用innerC)
但是我得到了错误Unknown column 'innerA' in 'field list'
,因为它不是列,这是有道理的。
如何使用别名innerA,innerB和innerC生成my_result?
编辑:
为解释我的意思,以下是查询的相关部分:
(select t.id from transaction t where loan_id = l.id and status_id = 61) as chargeback_transaction_id,
(select t.`datestamp` from transaction t where loan_id = l.id and status_id = 61) as chargeback_transaction_id_date,
(select ls.`datestamp` from loan_status ls where loan_id = l.id and status_id = 16 and datestamp < chargeback_transaction_id_date ORDER BY datestamp DESC LIMIT 1) as loan_closed_transaction_id_date_before_the_chargeback_aka_status_16,
/* Get date of next status 9, after the first 16: */
(select ls.`datestamp` from loan_status ls where loan_id = l.id and status_id = 9 and datestamp > loan_closed_transaction_id_date_before_the_chargeback_aka_status_16 ORDER BY datestamp ASC LIMIT 1) as date_of_next_status_9_after_the_first_16,
(select ls2.`datestamp` from loan_status ls2 INNER JOIN client clj ON (clj.`client_id` = ls2.`loan_id`) where clj.client_id = '3378228' and loan_id != l.id and ls2.status_id = 9 and datestamp > loan_closed_transaction_id_date_before_the_chargeback_aka_status_16 and datestamp < date_of_next_status_9_after_the_first_16 LIMIT 1) as other_loan_datestamp,
IF( other_loan_datestamp, loan_closed_transaction_id_date_before_the_chargeback_aka_status_16, date_of_next_status_9_after_the_first_16 ) AS close_date_to_use,
我猜这最后一部分(IF( other_loan_datestamp,
)完全没有意义,但是有没有办法用MySQL
中的别名来做这样的'if else'逻辑?
注意:
other_loan_datestamp
,loan_closed_transaction_id_date_before_the_chargeback_aka_status_16
和date_of_next_status_9_after_the_first_16
是类似于2018-05-25 12:31:16
的日期
答案 0 :(得分:0)
你有个主意
select if( i1.v, i2.v, i3.v ) as some_result
from (select 0 as v) i1, (select 2 as v) i2, (select 3 as v) i3
答案 1 :(得分:0)
在这种情况下,您不能使用别名。不幸的是,您将不得不编写整个内容。另一种方法是将其包装在子查询中。
SELECT a.*
, IF( a.other_loan_datestamp, a.loan_closed_transaction_id_date_before_the_chargeback_aka_status_16, a.date_of_next_status_9_after_the_first_16 ) AS close_date_to_use
FROM (
(select t.id from transaction t where loan_id = l.id and status_id = 61) as chargeback_transaction_id,
(select t.`datestamp` from transaction t where loan_id = l.id and status_id = 61) as chargeback_transaction_id_date,
(select ls.`datestamp` from loan_status ls where loan_id = l.id and status_id = 16 and datestamp < chargeback_transaction_id_date ORDER BY datestamp DESC LIMIT 1) as loan_closed_transaction_id_date_before_the_chargeback_aka_status_16,
/* Get date of next status 9, after the first 16: */
(select ls.`datestamp` from loan_status ls where loan_id = l.id and status_id = 9 and datestamp > loan_closed_transaction_id_date_before_the_chargeback_aka_status_16 ORDER BY datestamp ASC LIMIT 1) as date_of_next_status_9_after_the_first_16,
(select ls2.`datestamp` from loan_status ls2 INNER JOIN client clj ON (clj.`client_id` = ls2.`loan_id`) where clj.client_id = '3378228' and loan_id != l.id and ls2.status_id = 9 and datestamp > loan_closed_transaction_id_date_before_the_chargeback_aka_status_16 and datestamp < date_of_next_status_9_after_the_first_16 LIMIT 1) as other_loan_datestamp
) a