简体   繁体   中英

How can I conditionally use MYSQL aliases to generate a column in a query?

I know I can do this:

IF( 2 = 2 AND 0 = 0, 1, 0 ) AS some_result, (outputs 1 of course)

But I have the some inner selects in my query like so:

(select ... ) as innerA,
(select ... ) as innerB,
(select ... ) as innerC,

I then would like to do something like:

IF( innerA, innerB, innerC ) AS my_result, (if innerA then use innerB, otherwise innerC)

But I get the error Unknown column 'innerA' in 'field list' which makes sense because it isn't a column.

How can I use my aliases innerA, innerB and innerC to generate my_result?

EDIT :

In an attempt to explain what I mean, here is the relevant part of the query:

(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,

I guess this last part ( IF( other_loan_datestamp, ) is just completely meaningless but is there a way to do such 'if else' logic with aliases in MySQL ?

Note:

other_loan_datestamp , loan_closed_transaction_id_date_before_the_chargeback_aka_status_16 and date_of_next_status_9_after_the_first_16 are dates like 2018-05-25 12:31:16

You got an idea

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

You can't use alias in this case. You, unfortunately, will have to write the whole thing. Another way is to wrap it in subquery.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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