我有以下的数据库结构。
applets
--id (incr)
applet_stats
--id (incr)
--applet_id (relates to applet.id)
--date ("Y-m-d" format)示例数据:
applets
1
2
3
applet_stats
1 1 2015.01.15
2 1 2015.01.15
3 1 2015.01.15
4 1 2015.01.15
5 2 2015.01.14在上面的例子中,我们有3个applet。小程序1在2015.01.15有4x统计数据,小程序2在2015.01.14有1x统计数据(昨天)
如何查询数据库以获得以下输出:
// Show me the applets which doesn't have any statistics on "2015.01.15".
[2,3]我自己用leftJoin试过了,但我忽略了一些非常简单的东西,所以每次都会得到错误的结果。
Ps。在一个雄辩的声明中会更好。
发布于 2015-01-15 23:27:58
雄辩的方式,假设stats是hasMany关系:
$date = '2015-01-15';
Applet::whereDoesntHave('stats', function ($q) use ($date) {
$q->where('date', $date);
})->get();发布于 2015-01-15 23:24:51
SELECT id
from applets
where id not in(select distinct applet_id from applet_stats where date ='2015.01.15') 这可能会对你有帮助
https://stackoverflow.com/questions/27966660
复制相似问题