计数时间ID出现在表格中并返回行

时间:2010-04-29 07:09:40

标签: sql sql-server select count row

SELECT
Boats.id, Boats.date, Boats.section, Boats.raft, 
river_company.company, river_section.section AS river 
FROM Boats 
INNER JOIN river_company ON Boats.raft = river_company.id 
INNER JOIN river_section ON Boats.section = river_section.id 
ORDER BY Boats.date DESC, river, river_company.company

返回我需要的一切。但是我如何添加[Photos]表并计算Boats.id在其中出现的次数并将其添加到返回的行中。

所以,如果船只#17有5张照片我想要#17船的记录说PhotoCount = 5

7 个答案:

答案 0 :(得分:2)

我认为boatsId是Photos表中的一列?最简单的方法是使用子选择:

SELECT ...whatever columns...,

(SELECT COUNT(*) FROM Photos WHERE boatsId = Boats.id)

FROM ...rest of your query

答案 1 :(得分:2)

您可以将LEFT JOIN添加到子查询中,如下所示:

LEFT JOIN (SELECT COUNT(p.id) num, p.boatID FROM photos p GROUP BY p.boatID) 
          sub_photos ON (sub_photos.boatID = Boats.id)

然后在sub_photos.num字段中引用SELECT

它看起来像这样:

SELECT     Boats.id, Boats.date, Boats.section, Boats.raft, 
           river_company.company, river_section.section AS river,
           sub_photos.num AS number_of_photos
FROM       Boats 
INNER JOIN river_company ON Boats.raft = river_company.id 
INNER JOIN river_section ON Boats.section = river_section.id 
LEFT JOIN  (SELECT COUNT(p.id) num, p.boatID FROM photos p GROUP BY p.boatID) 
           sub_photos ON (sub_photos.boatID = Boats.id)
ORDER BY   Boats.date DESC, river, river_company.company

答案 2 :(得分:1)

SELECT Boats.id,
       Boats.date,
       Boats.section,
       Boats.raft,
       river_company.company,
       river_section.section AS river
       (SELECT count(*) FROM Photos WHERE boatsID = Boats.id)
FROM Boats
INNER JOIN river_company ON Boats.raft = river_company.id 
INNER JOIN river_section ON Boats.section = river_section.id 
ORDER BY Boats.date DESC, river, river_company.company

试试这个。

答案 3 :(得分:1)

另一种方式:

SELECT 
Boats.id, Boats.date, Boats.section, Boats.raft,  
river_company.company, river_section.section AS river  
Count(Photos.ID) AS PhotoCount
FROM Boats  
INNER JOIN river_company ON Boats.raft = river_company.id  
INNER JOIN river_section ON Boats.section = river_section.id  
OUTER JOIN Photos ON Boats.id = Photos.BoatID ORDER BY Boats.date DESC, river, river_company.company 
GROUP BY
    Boats.id, Boats.date, Boats.section, Boats.raft, river_company.company, river_section.section

答案 4 :(得分:1)

您尚未提供有关“照片”表格结构的任何信息,但此类内容适用于您。

SELECT
Boats.id, Boats.date, Boats.section, Boats.raft, 
river_company.company, river_section.section AS river,
PhotoCount.NumberOfBoats as PhotoCount 
FROM Boats 
INNER JOIN river_company ON Boats.raft = river_company.id 
INNER JOIN river_section ON Boats.section = river_section.id 
Left Join
(
    Select b2.Id, Count(b2.Id) as NumberOfBoats
    From Boats b2
    Join Photos p on b2.Id = p.BoatId
    Group By b2.Id
)BoatCount on BoatCount.Id = Boats.Id

ORDER BY Boats.date DESC, river, river_company.company

答案 5 :(得分:1)

另一种方式

SELECT DISTINCT
Boats.id, Boats.date, Boats.section, Boats.raft,  
river_company.company, river_section.section AS river,
count(photos.boatid) OVER(PARTITION BY photos.boatid)
FROM Boats  
INNER JOIN river_company ON Boats.raft = river_company.id  
INNER JOIN river_section ON Boats.section = river_section.id
LEFT JOIN photos on photos.boatid = Boats.id
ORDER BY Boats.date DESC, river, river_company.company 

答案 6 :(得分:1)

Select YourID, COUNT(*) FROM YourTable GROUP BY YourID

当我尝试根据ID或任何其他列跟踪记录计数可能不同的情况时,此功能正常。