Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server T-SQL组操作_Sql Server_Sql Server 2005_Tsql - Fatal编程技术网

Sql server T-SQL组操作

Sql server T-SQL组操作,sql-server,sql-server-2005,tsql,Sql Server,Sql Server 2005,Tsql,我有两个表,如下所示: **tblComments** Commentid addeddate Postid **tblPosts** Postid AddedBy AddedDate Title 我想找到每个帖子的评论总数。怎么办 select count(*), postid from tblPosts group by postid 我应该做我想做的事 应该做我认为应该做的事编辑:根据你下面的评论进行编辑,以获得所有帖子,包括那些评论为0的帖子。 试试看。我认为这应该像你期望的那

我有两个表,如下所示:

**tblComments**
Commentid
addeddate
Postid


**tblPosts**
Postid
AddedBy
AddedDate
Title
我想找到每个帖子的评论总数。怎么办

select count(*), postid from tblPosts group by postid 
我应该做我想做的事


应该做我认为应该做的事编辑:根据你下面的评论进行编辑,以获得所有帖子,包括那些评论为0的帖子。 试试看。我认为这应该像你期望的那样有效

SELECT p.Postid, p.Title, ISNULL(X.count,0)
FROM tblPosts p
LEFT OUTER JOIN 
   (SELECT postid, count(commentid) as Total
   FROM tblComments
   GROUP BY Postid) AS X
ON p.Postid = X.Postid

编辑:根据下面的评论进行编辑,以获取所有帖子,包括那些评论为0的帖子。 试试看。我认为这应该像你期望的那样有效

SELECT p.Postid, p.Title, ISNULL(X.count,0)
FROM tblPosts p
LEFT OUTER JOIN 
   (SELECT postid, count(commentid) as Total
   FROM tblComments
   GROUP BY Postid) AS X
ON p.Postid = X.Postid

您需要联接表以查找没有注释的帖子,从而生成零

select
    count(c.postid), P.postid --edit
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 

您需要联接表以查找没有注释的帖子,从而生成零

select
    count(c.postid), P.postid --edit
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 

要为无注释的post生成零,T-SQL:

select
    P.postid, count(c.postid) -- should not use COUNT(*) for LEFT JOINs
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 
对于Postgresql,获取行的基数:

select
    P.postid, count(c.*) -- should not use COUNT(*) for LEFT JOINs
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 
与此相关:

这就是为什么不应在左联接上使用COUNT(*)的原因:

create table posts
(
post_id int identity(1,1) not null primary key,
post varchar(max)
);

create table comments
(
post_id int not null references posts(post_id),
comment_id int identity(1,1) not null primary key,
comment varchar(max)
);

insert into posts(post) values('hello');

insert into posts(post) values('oh hai');
insert into comments(post_id,comment) values(SCOPE_IDENTITY(), 1);

-- don't
select p.post_id, COUNT(*) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id

-- do
select p.post_id, COUNT(c.post_id) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id
输出:

post_id     comment_count
----------- -------------
1           1
2           1


post_id     comment_count
----------- -------------
1           0
2           1
查询样式在Postgresql上更具吸引力,想想看,我们真正计算的是集合的基数,而不是列:


要为无注释的post生成零,T-SQL:

select
    P.postid, count(c.postid) -- should not use COUNT(*) for LEFT JOINs
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 
对于Postgresql,获取行的基数:

select
    P.postid, count(c.*) -- should not use COUNT(*) for LEFT JOINs
from
    tblPosts P 
    LEFT JOIN
    tblComments C On P.postid  = C.postid 
group by
    P.postid 
与此相关:

这就是为什么不应在左联接上使用COUNT(*)的原因:

create table posts
(
post_id int identity(1,1) not null primary key,
post varchar(max)
);

create table comments
(
post_id int not null references posts(post_id),
comment_id int identity(1,1) not null primary key,
comment varchar(max)
);

insert into posts(post) values('hello');

insert into posts(post) values('oh hai');
insert into comments(post_id,comment) values(SCOPE_IDENTITY(), 1);

-- don't
select p.post_id, COUNT(*) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id

-- do
select p.post_id, COUNT(c.post_id) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id
输出:

post_id     comment_count
----------- -------------
1           1
2           1


post_id     comment_count
----------- -------------
1           0
2           1
查询样式在Postgresql上更具吸引力,想想看,我们真正计算的是集合的基数,而不是列:


谢谢..对于那些没有评论的帖子,如何将0放入them@Bujo,查看我的查询:-)和相关文章谢谢..对于那些没有评论的帖子,如何将0放入them@Bujo,查看我的查询:-)和相关文章谢谢..对于那些没有评论的帖子,如何将0放入them@gbn完全…我也更喜欢你的查询!!:-)谢谢..对于那些没有评论的帖子,如何将0放入them@gbn完全…我也更喜欢你的查询!!:-)你能证明为什么不为左连接计数(*)吗?@gbn对我来说听起来像是迷信:这不是迷信,如果你使用左连接,即使
帖子
没有任何相应的
注释
,它仍然会产生1的计数。你试试看。你试着读一篇文章为什么我也不相信使用COUNT(1),我鼓励使用
COUNT(*)
COUNT(table.*)
(如果在Postgresql上)这篇文章:哦,是的,我在解释你的代码注释时太仓促了。你能证明为什么不使用COUNT(*)进行左连接吗?@gbn对我来说听起来像是迷信:这不是迷信,如果你使用左连接,即使
帖子
没有任何相应的
注释
,它仍然会产生1的计数。你试试看。你试着读这篇文章为什么我也不相信使用COUNT(1),我鼓励使用
COUNT(*)
COUNT(table.*)
(如果在Postgresql上)这篇文章:哦,是的,我在解释你的代码注释时太仓促了,即使TBLPost没有任何注释,查询仍然会产生1的计数。将
计数(*)
更改为
计数(c.postid)
。如果在Postgres上,将
计数(*)
更改为
计数(c.*)
。如果你这样做,我会投票给你并删除我的答案。下面是一个完整的例子:即使TBLPost没有任何注释,该查询仍然会产生1的计数。将
计数(*)
更改为
计数(c.postid)
。如果在Postgres上,将
计数(*)
更改为
计数(c.*)
。如果你这样做,我会投票给你,并删除我的答案以下是完整的例子: