簡體   English   中英

PHP - MySQL IF(布爾)條件

[英]PHP - MySQL IF (boolean) condition

對不起,我不知道如何標題這個問題,但我會盡力解釋我的問題。 我的數據庫中有兩個表,如下所示:

APPLICATIONS:

app_id | data1 | data2
----------------------

1      | foo   | foo
2      | bar   | bar




APP_REQUIREMENTS:

app_id | requirement  | is_satisfied
--------------------------------------
1      | requirement1 | false
1      | requirement2 | false
2      | requirement1 | true
2      | requirement2 | true

我想要做的是查詢我的數據庫以獲取APPLICATIONS表中的所有信息以及一個額外的字段,該字段表示該應用程序是否有任何UNSATISFIED要求,因此我的查詢將返回如下內容:

app_id | data1 | data2 | meets_all_requirements
------------------------------------------------
1      | foo   | foo   | false
2      | bar   | bar   | true

使用一個查詢執行此操作的最佳方法是什么? 是否有更好的方法來設置我的表/關系以適應這種情況?

任何意見是極大的贊賞!

假設is_satisfied是一個布爾字段,然后min()有效執行一次and在所有的條件:

select a.*, min(is_satisfied) as all_satisfied
from Application a left outer join
     App_Requirements ar
     on a.app_id = ar.app_id
group by a.app_id;

如果值實際上是字符串,則可以執行以下操作:

select a.*, min(is_satisfied = 'true') as all_satisfied
from Application a left outer join
     App_Requirements ar
     on a.app_id = ar.app_id
group by a.app_id;
select a.*, sum(not r.is_satisfied) = 0 as meets_all_requirements
from applications a
left join app_requirements r on a.app_id = r.app_id
group by a.app_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM