里面的SELECT里面

时间:2016-06-29 03:20:58

标签: php mysql sql laravel

是否可以在SQL中执行类似的操作?

SELECT SUM(jobRequirements.number) as total, 
    SUM(jobRequirements.number WHERE jobRequirements.type=1) as jobIT, 
    SUM(jobRequirements.number WHERE jobRequirements.type=2) as jobTourism FROM jobRequirements

我在内部联接也使用where子句,但我是否必须使用每个jobRequirements.type的where子句进行单独查询,或者可以在一个查询中完成?

我的完整查询来自Laravel,它看起来像这样:

$jobStats = DB::table('jobRequirements')
            ->join('jobs', 'jobRequirements.job', '=', 'jobs.id')
            ->join('types', 'jobRequirements.type', '=', 'types.id')
            ->join('users', 'jobs.user', '=', 'users.id')
            ->select(DB::raw('SUM(jobRequirements.number) as total, users.name'))
            ->where('jobRequirements.active', 1)
            ->orderBy('users.name', 'asc')
            ->groupBy('users.id')
            ->get();

有了这个,我得到总数,但我需要按类型得到数字的总和。

更新

有了这个,经过一点修改,代码来自@Terminus

SELECT SUM(jobReq.number) as total, users.name as user, types.name as type
FROM jobRequirements jobReq
INNER JOIN jobs j ON j.id = jobReq.job
INNER JOIN types t ON t.id = jobReq.type
INNER JOIN users u ON u.id = j.user
WHERE jobReq.active = 1
GROUP BY u.id, t.id
ORDER BY u.name ASC

我明白了:

total  |  user  |  type
------------------------
2      |  user1 |  type2
5      |  user2 |  type6
1      |  user1 |  type3
3      |  user2 |  type2

我最终需要的是:

       | type2 | type3  | type6 | total
---------------------------------------
user1  | 2     | 1      | 0     | 3
user2  | 3     | 0      | 5     | 8
total  | 5     | 1      | 5     | 11

3 个答案:

答案 0 :(得分:1)

SELECT SUM(jobRequirements.number) as total, 
    SUM(CASE WHEN jobRequirements.type=1 THEN jobRequirements.number ELSE 0 END) as jobIT, 
    SUM(CASE WHEN jobRequirements.type=2 THEN jobRequirements.number ELSE 0 END) as jobTourism 
FROM jobRequirements

答案 1 :(得分:1)

答案很长,简短:

将对groupBy的通话更改为:

->groupBy('users.id', 'types.id')

当然,现在您将开始为每个用户返回更多行。

答案很长,很长:

因此,Laravel可能正在构建类似于下面的查询。 (请注意,jobReqjtu被称为别名,这基本上可以为您节省大量的打字空间。

SELECT SUM(jobReq.number) as total, users.name
FROM jobRequirements jobReq
INNER JOIN jobs j ON j.id = jobReq.job
INNER JOIN types t ON t.id = jobReq.type
INNER JOIN users u ON u.id = jobReq.user
WHERE jobReq.active = 1
GROUP BY u.id
ORDER BY u.name ASC

要按user.idtype.id订购,您可以将t.id添加到GROUP BY子句中,如下所示:

SELECT SUM(jobReq.number) as total, users.name
FROM jobRequirements jobReq
INNER JOIN jobs j ON j.id = jobReq.job
INNER JOIN types t ON t.id = jobReq.type
INNER JOIN users u ON u.id = jobReq.user
WHERE jobReq.active = 1
GROUP BY u.id, t.id
ORDER BY u.name ASC

新修改

SQL中的PIVOT命令之类的东西可能会让你得到你想要的东西,但我讨厌尝试编写这些语句。在我不熟悉的意见中,更简单的解决方案是循环查询结果,构建一个数组,以一种格式保存数据,您可以在显示时使用不同的循环进行循环。下面:

$jobStats = DB::table('jobRequirements')
            ->join('jobs', 'jobRequirements.job', '=', 'jobs.id')
            ->join('types', 'jobRequirements.type', '=', 'types.id')
            ->join('users', 'jobs.user', '=', 'users.id')
            ->select(DB::raw('users.name, types.id, SUM(jobRequirements.number) as total'))
            ->where('jobRequirements.active', 1)
            ->orderBy('users.name', 'asc')
            ->orderBy('types.id')
            ->groupBy('users.id')
            ->get();


// gather the values into an array
$displayJobStatus = array();
$allTypes = array();
foreach($jobStats as $stat) {
  if(array_search($stat['type'], $allTypes) === false) {
    $allTypes[] = $stat['type']; // we need to iterate over this later
  }
  $displayJobStatus[$stat['name']][$stat['type']] = $stat['type'];
}

$allTypes = sort($allTypes);

echo 'User Name | ' . implode(' | ', $allTypes) . ' | Total';
foreach($displayJobStatus as $userName => $typeTotals) {
  echo $userName . ' | ';
  $total = 0;
  foreach($allTypes as $type) {
    if(array_key_exists($type, $typeTotals)) {
      echo $typeTotals[$type] . ' | ';
      $total += $typeTotals[$type];
    }
  }
  echo $total . "\n";
}

你可以从这里把管道(|)变成html表格标签我觉得很简单:)

答案 2 :(得分:-2)

您无法在同一查询中使用2 where子句

  SELECT SUM(jobRequirements.number) as total, 
jobRequirements.number WHERE jobRequirements.type=1 as jobIT, 
jobRequirements.number WHERE jobRequirements.type=2 as jobTourism FROM jobRequirements

然而,你可以(仅举例)

    SELECT
         (SELECT SUM(jobRequirements.number)) as total, 
          jobRequirements.number
    From
          jobRequirements WHERE jobRequirements.type=2 

提示,我真的不知道这意味着什么。你也可以做联盟:)(不是洋葱而不是那些有抗议迹象的家伙)