我想检查哪些用户在数据库中拥有最多记录。因此每个用户都有一个特定的Id,并且该Id在几个表中用作参考。
有几个表包含一个UserId列,如表Exams,Answers,Questions,Classes等。是否可以在所有这些表中计算具有特定UserId的所有记录?
答案 0 :(得分:1)
我会这样做:
var inp = $("#txt");
// where #txt is the id of the textbox
$(".table-cell-text").keyup(function (event) {
if (event.keyCode == 13) {
if (inp.val().length > 0) {
$('#myTable tr:last').replaceWith('<tr class="table-row"><td class="table-cell">' + inp.val() + '</td>' +
'<td class="table-cell">' +
'<td></td>' +
'</td>' +
'<td class="table-cell">' +
'</td></tr>');
}
}
});
如果您只想要计数,则不需要表名,只是您想知道来源的情况。
答案 1 :(得分:1)
;with cte as (
select rowsCount = count(*) from A where UserId = 1
union all
select rowsCount = count(*) from B where UserId = 1
union all
select rowsCount = count(*) from C where UserId = 1
)
select sum(rowsCount) from cte
答案 2 :(得分:1)
试试这个,
SELECT Count(E.ID) + Count(A.ID) + Count(Q.ID) + Count(C.ID) AS SumofAllCount
FROM dbo.Exams E
LEFT JOIN dbo.Answers A ON A.UserId = E.UserId
LEFT JOIN dbo.Questions Q ON Q.UserId = E.UserId
LEFT JOIN dbo.Classes C ON C.UserId = E.UserId
WHERE E.UserId = @YouruserID
GROUP BY E.UserId