Я знаю, что я могу сделать это:
IF( 2 = 2 AND 0 = 0, 1, 0 ) AS some_result,
(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?
EDIT:
В попытке объяснить, что я имею в виду, вот соответствующая часть запроса:
(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
?
Замечания:
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
В этом случае вы не можете использовать псевдоним. Вам, к сожалению, придется написать все это. Другой способ - обернуть его в подзапрос.
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
У вас есть идея
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