作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,我不确定如何命名这个问题,但我会尽力解释我的问题。我的数据库中有两个表,如下所示:
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
我想要做的是查询我的数据库以获取应用程序表中的所有信息以及一个额外字段,该字段表示该应用程序是否有任何未满足的要求,因此我的查询将返回如下所示的内容:
app_id | data1 | data2 | meets_all_requirements
------------------------------------------------
1 | foo | foo | false
2 | bar | bar | true
通过一个查询执行此操作的最佳方法是什么?有没有更好的方法来设置我的表/关系来适应这种情况?
非常感谢任何建议!
最佳答案
假设is_satisfied
是一个 bool 字段,那么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;
关于PHP - MySQL IF( bool )条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17935290/
我是一名优秀的程序员,十分优秀!