2012-06-06 99 views
0

我有一個叫做發票表。以幫助使這個問題簡單,我們會說,有發票4列。Mysql的默認設置爲空,如果多行不匹配

id     (PK auto int) 
booking_id   (int) 
is_business_invoice (1 or 0 or NULL) 
amount    (decimal) 

預訂可以有多個發票。

林tryng編寫一個查詢將決定是否對一個給定的預約ID都是做生意的發票或個人發票。如果有不同is_business_invoice類型的多張發票,則默認爲空。如果有3個預訂和所有3 is_business_invoice那麼它將返回1

回答

1
select case when count(is_business_invoice) = sum(is_business_invoice) 
      then 1 
      when sum(is_business_invoice) = 0 
      then 0 
      else null 
     end as IsBusinessInvoices 
from invoices 
group by booking_id 
相關問題