2017-05-28 92 views
1
select 
count(invoice.id) as count_invoice_id, // i want to simply use count_invoice_id instead of using count(discount_offer.id) again inside the if statement below 
if(count(invoice.id) > 1 , 'true','false') // i want to used count_invoice_id here 
from invoice as invoice 
left join discount_offer as discount_offer 
on invoice.id = discount_offer.invoice_id; 

以上是我的sql,如果我想在if語句中使用count_invoice_id,是否有可能?目前我所做的是在invoice.id列再次重複計數方法。如何在select語句中使用mysql變量/如何在下一次select中使用'as'變量

我想從改變 '計數(invoice.id)' 到 'count_invoice_id' if語句內的SQL上述

回答

3

你可以使用子查詢:

SELECT count_invoice_id, IF(count_invoice_id > 1 , 'true', 'false') 
FROM (SELECT COUNT(invoice.id) AS count_invoice_id 
     FROM  invoice 
     LEFT JOIN discount_offer ON invoice.id = discount_offer.invoice_id) t 
+1

感謝,這是工作,但是還有其他方法可以實現嗎? –

相關問題