让我们说我有2张桌子
resume_profiles表:
user_id current_location city
| 3 | | Chennai | | Kolkatta |
| 4 | | Mumbai | | Ahmaedabad |
| 5 | | Pune | | Kolkatta |
| 6 | | Kolkatta | | Pune |
resume_locations表:
user_id location
| 2 | | Chennai |
| 2 | | Mumbai |
| 3 | | Pune |
| 4 | | Kolkatta |
我需要将这些结果表位置组合到一个SET中,其中resume_profiles.user_id = resume_locations.user_id使用Union ALL,所以我有这样的事情:
City Aggregate
| Chennai | | 1 |
| Mumbai | | 1 |
| Pune | | 2 |
| Kolkatta | | 3 |
| Ahmaedabad | | 1 |
工作查询没有user_id:
$preferred_locations = ResumeLocation::selectRaw('location as city');
$current_locations = ResumeProfile::selectRaw('current_location as city');
$subquery = ResumeProfile::selectRaw('city')
->unionAll($current_locations)
->unionAll($preferred_locations);
$locations = DB::table(DB::raw("({$subquery->toSql()}) AS s"))
->selectRaw('s.city,count(*) as aggregate')
->groupBy('s.city')
->get();
**在使用条件时在查询中获取异常:**
$preferred_locations = ResumeLocation::selectRaw('location as city')
->where('resume_locations.user_id','resume_profiles.user_id');
$current_locations = ResumeProfile::selectRaw('current_location as city');
$subquery = ResumeProfile::selectRaw('city')
->unionAll($current_locations)
->unionAll($preferred_locations);
$locations = DB::table(DB::raw("({$subquery->toSql()}) AS s"))
->selectRaw('s.city,count(*) as aggregate')
->groupBy('s.city')
->get();
我不知道如何在表格中实现此where
条件。
答案 0 :(得分:1)
<强>其中强>:
$preferred_locations = ResumeLocation::selectRaw('location as city')
->whereIn('user_id', function($q) {
$q->select('user_id')->from('resume_profiles');
});