[英]Laravel query with union and count
I have the queries: 我有疑问:
$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();
And I have the table, look like this: Table matches
: 我有桌子,看起来像这样:桌子
matches
:
And table occurrences
: 和表
occurrences
:
I don't know why, but my query does not work correctly. 我不知道为什么,但是我的查询无法正常工作。 My query returns
2
, but should return 4
. 我的查询返回
2
,但应返回4
。
You can use Laravel Debugbar to view the actual queries that are run and verify that they are what you want. 您可以使用Laravel Debugbar查看运行的实际查询并验证它们是否是您想要的。 I would also run the whole thing as one query.
我也将整个事情作为一个查询来运行。
I would also normalize the database a bit more. 我还将对数据库进行更多标准化。 Add a model URL and a corresponding urls table with id, match_id, url, accepted.
添加一个模型URL和一个带有ID,match_id,URL的URL表。
It looks like you are forgetting a 看来您正在忘记
->get()
for your first query. ->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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.