2017-05-28 5 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'を使う方法

私はあなたがサブクエリを使用することができ

答えて

3

上記のSQLのif文の内側 'count_invoice_id' から '数(invoice.id)' から変更したい:それは働いて

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

おかげで、これを達成する他の方法はありますか? –

関連する問題