SQL计数()/左联接?
我有三个表:calls、attachments和notes,我想显示calls表中的所有内容,还想显示通话是否有附件以及通话是否有notes。-通过确定是否存在包含call_id的附件或便笺记录。可能有笔记和附件,也可能没有,但我需要知道 表结构: 电话:SQL计数()/左联接?,sql,tsql,count,left-join,Sql,Tsql,Count,Left Join,我有三个表:calls、attachments和notes,我想显示calls表中的所有内容,还想显示通话是否有附件以及通话是否有notes。-通过确定是否存在包含call_id的附件或便笺记录。可能有笔记和附件,也可能没有,但我需要知道 表结构: 电话: call_id | title | description 附件: attach_id | attach_name | call_id 注: note_id | note_text | call_id
call_id | title | description
附件:
attach_id | attach_name | call_id
注:
note_id | note_text | call_id
如果我写:
SELECT c.call_id
, title
, description
, count(attach_id)
FROM calls c
LEFT JOIN attachments a ON c.call_id = a.call_id
GROUP BY c.call_id
, title
, description
给我所有电话的列表和附件的数量
我怎样才能在一个有注释数的列中添加注释,或者在一个表示有注释的列中添加注释
有什么想法吗
谢谢。谢谢你的计数
或者为了生存(如果这是你所需要的,会更有效率)
如果fiels 4或fiels 5中存在呼叫id,则您知道您有附件或便条
如果您需要填写附件或注释的编号,请查看其他答案,查看AtaTheDev的帖子。在计数中使用distinct
您必须使用distinct in计数,因为您的组由两个不同的实体增长。因此,您只需计算每一个的不同值。下一个查询将返回两个计数以及bit
值,无论是否存在任何附件和注释
select
c.call_id, c.title, c.description,
count(distinct a.attach_id) as attachments_count,
count(distinct n.note_id) as notes_count,
/* add these two if you need to */
case when count(distinct a.attach_id) > 0 then 1 else 0 end as has_attachments,
case when count(distinct n.note_id) > 0 then 1 else 0 end as has_notes
from calls c
left join attachments a
on (a.call_id = c.call_id)
left join notes n
on (n.call_id = c.call_id)
group by c.call_id, c.title, c.description
我想应该是这样的
选择c.呼叫id、标题、描述、计数(不同的附加id)、计数(不同的注释id)
来自呼叫c
在c.call\u id=a.call\u id上左连接附件a
n.call\u id=a.call\u id上的左连接注释n
按c.call\u id、标题、描述分组
这也适用于:
SELECT
cl.*,
(SELECT count(1) FROM attachments AS at WHERE at.call_id = cl.id) as num_attachments,
(SELECT count(1) FROM notes AS nt WHERE nt.call_id = cl.id) as num_notes,
FROM calls AS cl
我使用了这个简单的查询。此查询允许您轻松使用主表列,而无需分组依据
Select StudentName,FatherName,MotherName,DOB,t.count from Student
left JOIN
(
Select StudentAttendance.StudentID, count(IsPresent) as count
from StudentAttendance
group by StudentID, IsPresent
) as t
ON t.StudentID=Student.StudentID
如果只在
exists()
语句中选择主键而不是所有列*
,不是更快吗?@Robert-不。这是一个神话。@Martin:但这是一个非常好的实践,特别是如果在客户端使用EF,它确实会产生巨大的差异。嘿,所有回答都很好的家伙。这是第一个发布的,我将在这里使用你的有效的一个,马丁-但我将使用罗伯特提到的主键。谢谢大家。哦,我不得不把它改为“WHERE n.call\u id=c.call\u id”。投票反对的人应该提供一个评论。我不明白为什么我的答案不正确?
select
c.call_id, c.title, c.description,
count(distinct a.attach_id) as attachments_count,
count(distinct n.note_id) as notes_count,
/* add these two if you need to */
case when count(distinct a.attach_id) > 0 then 1 else 0 end as has_attachments,
case when count(distinct n.note_id) > 0 then 1 else 0 end as has_notes
from calls c
left join attachments a
on (a.call_id = c.call_id)
left join notes n
on (n.call_id = c.call_id)
group by c.call_id, c.title, c.description
SELECT
cl.*,
(SELECT count(1) FROM attachments AS at WHERE at.call_id = cl.id) as num_attachments,
(SELECT count(1) FROM notes AS nt WHERE nt.call_id = cl.id) as num_notes,
FROM calls AS cl
Select StudentName,FatherName,MotherName,DOB,t.count from Student
left JOIN
(
Select StudentAttendance.StudentID, count(IsPresent) as count
from StudentAttendance
group by StudentID, IsPresent
) as t
ON t.StudentID=Student.StudentID