使用union和count的Laravel查询

时间:2017-04-27 16:38:57

标签: php sql laravel union

我有疑问:

$dateToday = date('Y-m-d',strtotime(Carbon::today()));

$countTodayMatchOccurrences = DB::table('occurrences')->select('date')->whereDate('date', '=', $dateToday);
$countTodayMatch = DB::table('matches')->select('date')->whereDate('date', '=', $dateToday)->union($countTodayMatchOccurrences)->count();

我有桌子,看起来像这样: 表matches

Matches table

occurrences

occurrences

我不知道为什么,但我的查询无效。我的查询返回2,但应返回4

2 个答案:

答案 0 :(得分:0)

您可以使用Laravel Debugbar查看正在运行的实际查询,并验证它们是否符合您的要求。我也会将整个事情作为一个查询运行。

我还会将数据库标准化一点。添加模型URL和相应的urls表,其中包含id,match_id,url,accepted。

答案 1 :(得分:-1)

看起来你忘记了

第一次查询

->get()

$dateToday = date('Y-m-d',strtotime(Carbon::today()));

$countTodayMatchOccurrences = DB::table('occurrences')->select('date')->whereDate('date', '=', $dateToday)->get();
$countTodayMatch = DB::table('matches')->select('date')->whereDate('date', '=', $dateToday)->union($countTodayMatchOccurrences)->count();