我试图根据分配的文件计算多个用户,并按这些文件进行分组'具有以下MySQL查询的状态:
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head runat="server">
<title></title>
<script src="js/jquery-2.1.3.min.js"></script>
<link href="css/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css" rel="stylesheet" />
<script src="js/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('MyCtrl1', function ($scope) {
$scope.today = new Date();
});
app.controller('MyCtrl2', function ($scope) {
$scope.myData = [{ name: "Moroni", age: 50 },
{ name: "Tiancum", age: 43 },
{ name: "Jacob", age: 27 },
{ name: "Nephi", age: 29 },
{ name: "Enos", age: 34 }];
$scope.gridOptions = { data: 'myData' };
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-controller="MyCtrl1">
<div class="jumbotron">
</div>
<div class="container container-fluid">
<div class="row">
<div class="col-lg-offset-2 col-lg-4">
<div class="table table-hover" ng-grid="gridOptions"></div>
</div>
</div>
<p class="text-right">{{ today | date }}</p>
</div>
</div>
<div ng-controller="MyCtrl2">
<div class="table table-hover" ng-grid="gridOptions"></div>
</div>
</form>
</body>
</html>
我的问题是,如果没有用户拥有某个状态的文件,查询将不会返回该行:
目前查询返回的值如下:
SELECT
(CASE WHEN F.status IS null THEN 'records'
WHEN F.status = 0 THEN 'prospects'
WHEN F.status >= 1 AND F.status < 4 THEN 'open'
WHEN F.status = 4 THEN 'archived' END) AS FileStatus,
count(DISTINCT U.id) as users
FROM
users_table U
LEFT OUTER JOIN files_table as F on (F.user_id = U.id or F.user_id IS NULL)
WHERE U.team = 1
GROUP BY FileStatus DESC
但是我想要显示零或空值,如下所示:
+------------+-------+
| FileStatus | users |
+------------+-------+
| prospects | 5 |
| open | 10 |
| archived | 12 |
+------------+-------+
我确定这非常简单,但任何帮助都会非常受欢迎✌️
答案 0 :(得分:1)
要获取所有文件状态,您需要有一个包含这些状态的表。如果不可用,您可以使用子查询内联生成它。
其次,用户团队的条件应该是连接条件的一部分,否则(当你将它放在where
子句中时)它会将外连接转换为内连接。
关于“记录”计数的评论之后,无论如何使用外部联接都没有任何意义。我建议为该类别建议union
:
SELECT FileStatus,
count(DISTINCT U.id) as users
FROM (SELECT 0 as status, 'prospects' as FileStatus
UNION ALL SELECT 1, 'open'
UNION ALL SELECT 2, 'open'
UNION ALL SELECT 3, 'open'
UNION ALL SELECT 4, 'archived') as C
INNER JOIN files_table as F
on F.status = C.status
INNER JOIN users_table U
on F.user_id = U.id
and U.team = 1
GROUP BY FileStatus DESC
UNION ALL
SELECT 'records',
count(*)
FROM users_table
WHERE id NOT IN (SELECT user_id FROM files_table)
AND team = 1
如果要使用范围,则在子查询中返回低/高端,并相应地调整连接条件:
SELECT FileStatus,
count(DISTINCT U.id) as users
FROM (SELECT 0 as statusFrom, 0 statusTo, 'prospects' as FileStatus
UNION ALL SELECT 1, 3, 'open'
UNION ALL SELECT 4, 4, 'archived') as C
INNER JOIN files_table as F
on F.status between C.statusFrom and C.statusTo
INNER JOIN users_table U
on F.user_id = U.id
and U.team = 1
GROUP BY FileStatus DESC
UNION ALL
SELECT 'records',
count(*)
FROM users_table
WHERE id NOT IN (SELECT user_id FROM files_table)
AND team = 1