2014-07-22 33 views
0

我需要篩選具有如下一些條件列查詢:Mysql的選擇alias列

SELECT 
    `c`.`fullname` AS `fullname`, 
    IFNULL(`c`.`new_ic`,`c`.`old_ic`) AS `Ic`, 
    `bla`.`loan_no` AS `Loan_No`, 
    `bla`.`total_loan_amt` AS `Total_Loan_Amt`, 
    `bla`.`monthly_installment` AS `Month_Inst_Amt`, 
    `bla`.`disb_date` AS `disb_date` FROM (`customers` `c` 
JOIN `basic_loan_application` `bla` 
    ON ((`bla`.`customer_uid` = `c`.`customer_uid`))) 
WHERE Ic LIKE '%800%' ORDER BY `bla`.`Basic_Loan_Application_Id` DESC LIMIT 0, 10 

但我不能使用IC領域,併爲下面的描述下返回錯誤:

Unknown column 'Ic' in 'where clause' 

我如果可能,要儘量避免嵌套選擇

有沒有解決這個問題的另一種方法?

回答

0

只是重複定義:

WHERE IFNULL(`c`.`new_ic`,`c`.`old_ic`) LIKE '%800%' 
ORDER BY `bla`.`Basic_Loan_Application_Id` DESC 
LIMIT 0, 10 

或者,如果你喜歡,可以使用MySQL的擴展,它允許您使用having條款做過濾(在這種情況下):

HAVING Ic LIKE '%800%' 
ORDER BY `bla`.`Basic_Loan_Application_Id` DESC 
LIMIT 0, 10