SQL 查询从另一个 SQL 查询中选择 COUNT

发布于 2024-11-26 22:32:54 字数 856 浏览 1 评论 0原文

我在进行 SQL SERVER 2000 查询时遇到了一些问题。这是我的场景:

  • 我有一个名为 Folders 的表,其中包含 3 列:pk_folderIDfolderNamefk_userID >。

  • 此外,我还有另一个名为 FolderMedia 的表,它存储属于某个文件夹的媒体(无论什么)。有 2 列:fk_folderIDfk_media

  • 最后,我有一个名为 Media 的表,它存储一些媒体详细信息。它有一个主键pk_media,在其他列中,它有一个MediaType列,它告诉媒体的类型:图像或视频。

现在,我想要一个执行以下操作的查询:

选择属于某个 fk_userID 的所有文件夹,然后获取该文件夹中的媒体数量。我在 StackOverflow 上看到过这样的查询,但我没有设法升级它以获取 2 个媒体计数(基于其类型)

基本上,获取属于的所有文件夹的文件夹详细信息(名称等)用户(fk_userID),并且对于每个文件夹,获取其中的图像和视频的数量(作为单独的值)。

选择基本上会返回: 文件夹名称、计数(文件夹中的图像)、计数(文件夹中的视频)、其他文件夹详细信息。

一个明显的解决方案是获取所有文件夹,然后手动计算其中的图像/视频数量......但我首先想尝试查询。

谢谢你,

I'm having a little trouble making an SQL SERVER 2000 query. Here is my scenario:

  • I have a table called Folders with 3 columns: pk_folderID, folderName and fk_userID.

  • Also, I have another table called FolderMedia which stores what media (whatever) belong to a certain folder. There are 2 columns: fk_folderID, fk_media.

  • And the last, I have a table called Media which stores some media details. It has a primary key pk_media and among other columns, it has a MediaType column which tells the type of that media: image or video.

Now, I would like a query that does the following:

Select all folders that belong to a certain fk_userID, and then also get the number of media in that folder. I've seen a query like this here on StackOverflow, but I didn't manage to upgrade it to get 2 counts of media (based on their type)

Basically, get the folder details (name, etc) for all folders that belong to a user(fk_userID) and also, for each folder get the number of images and videos in it (as separate values).

The select would basically return:
folderName, count(images in folder), count(videos in folder), other folder details.

One obvious solution would be to just get all folders and then manually calculate the number of images/videos in them... but I would first like to try with a query.

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岛歌少女 2024-12-03 22:32:54

基本上是这样的:

SELECT
  f.pk_folderID,
  f.folderName,
  VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
  ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END)
FROM Folder f
  LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
  LEFT JOIN Media m ON fm.fk_media = m.pk_media
WHERE f.fk_userID = @UserID
GROUP BY
  f.pk_folderID,
  f.folderName

更新(基于附加请求):

要将某种TOP 1 Media.Name包含到结果集中,可以将上面的查询更改为this:

SELECT
  f.pk_folderID,
  f.folderName,
  VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
  ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END),
  MediaName  = MAX(CASE fm.timestamp WHEN t.timestamp THEN m.Name END)
FROM Folder f
  LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
  LEFT JOIN Media m ON fm.fk_media = m.pk_media
  LEFT JOIN (
    SELECT
      fk_folderID,
      timestamp = MIN(timestamp)
    FROM FolderMedia
    GROUP BY fk_folderID
  ) t ON fm.fk_folderID = t.fk_folderID AND fm.timestamp = t.timestamp
WHERE f.fk_userID = @UserID
GROUP BY
  f.pk_folderID,
  f.folderName

如果最小 FolderMedia.timestamp 值在其文件夹内不唯一,则相应 Media.Name 的最终值将由其字母顺序排序决定。特别是,上面的查询选择了集合中的最后一个(使用 MAX())。

Basically something like this:

SELECT
  f.pk_folderID,
  f.folderName,
  VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
  ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END)
FROM Folder f
  LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
  LEFT JOIN Media m ON fm.fk_media = m.pk_media
WHERE f.fk_userID = @UserID
GROUP BY
  f.pk_folderID,
  f.folderName

UPDATE (based on the additional request):

To include a sort of TOP 1 Media.Name into the result set, the above query could be changed like this:

SELECT
  f.pk_folderID,
  f.folderName,
  VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
  ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END),
  MediaName  = MAX(CASE fm.timestamp WHEN t.timestamp THEN m.Name END)
FROM Folder f
  LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
  LEFT JOIN Media m ON fm.fk_media = m.pk_media
  LEFT JOIN (
    SELECT
      fk_folderID,
      timestamp = MIN(timestamp)
    FROM FolderMedia
    GROUP BY fk_folderID
  ) t ON fm.fk_folderID = t.fk_folderID AND fm.timestamp = t.timestamp
WHERE f.fk_userID = @UserID
GROUP BY
  f.pk_folderID,
  f.folderName

In cases where minimal FolderMedia.timestamp values are not unique within their folders, the ultimate value of the corresponding Media.Name will be decided by its alphabetical sorting. In particular, the above query selects the last one of the set (with MAX()).

网白 2024-12-03 22:32:54

Folders 表中获取所需的所有数据,将其与 FolderMediaMedia 左侧连接
使用 sumcase 来计算所有视频和图像的数量。

SUM(CASE WHEN mediaTypeId = videoId THEN 1 ELSE 0 END) as videoCount

Get all data you need from Folders table left join it with FolderMedia and Media
use sum with case inside to count all videos and images.

SUM(CASE WHEN mediaTypeId = videoId THEN 1 ELSE 0 END) as videoCount

〗斷ホ乔殘χμё〖 2024-12-03 22:32:54

//选择文件夹名称,计数(当Folders.pk_folderID为1时,否则为空结束)

count(SELECT FolderMedia.fk_media from FolderMedia JOIN Media on fk_media=pk_media where image is not null) as nrImg 

等..

//select folderName ,count(case when Folders.pk_folderID then 1 else null end)

count(SELECT FolderMedia.fk_media from FolderMedia JOIN Media on fk_media=pk_media where image is not null) as nrImg 

etc..

朕就是辣么酷 2024-12-03 22:32:54

这是带有一些示例数据的查询。希望有帮助。

declare @Folders table (pk_folderID int, folderName varchar(32), fk_userID int)
declare @Media table (pk_media int, name varchar(50), type varchar(32))
declare @FolderMedia table (fk_folderID int, fk_media int)

insert into @Folders values (1, 'Folder1', 1000)
insert into @Folders values (2, 'Folder2', 1000)
insert into @Folders values (3, 'Folder1', 2000)
insert into @Folders values (4, 'Folder1', 2000)

insert into @Media values (1, 'graph.jpg', 'image')
insert into @Media values (2, 'timer.jpg', 'image')
insert into @Media values (3, 'timer1.jpg', 'image')
insert into @Media values (4, 'harry_potter.mpeg', 'video')
insert into @Media values (5, 'harry_potter1.mpeg', 'video')
insert into @Media values (6, 'harry_potter2.mpeg', 'video')

insert into @FolderMedia values (1, 1)
insert into @FolderMedia values (1, 3)
insert into @FolderMedia values (1, 6)
insert into @FolderMedia values (2, 2)
insert into @FolderMedia values (2, 4)

select folderName, fk_userID, imageData.imgCount, videoData.videoCount from 
@Folders
left outer join
(
    select fk_folderID, COUNT(*) as imgCount
    from @FolderMedia
    inner join @Media 
    on fk_media = pk_media
    and type = 'image'
    group by fk_folderID
) as imageData
on imageData.fk_folderID = pk_folderID
left outer join
(
    select fk_folderID, COUNT(*) as videoCount
    from @FolderMedia
    inner join @Media 
    on fk_media = pk_media
    and type = 'video'
    group by fk_folderID
) as videoData
on videoData.fk_folderID = pk_folderID
where fk_userID = 1000

Here is a query with some example data. Hope it helps.

declare @Folders table (pk_folderID int, folderName varchar(32), fk_userID int)
declare @Media table (pk_media int, name varchar(50), type varchar(32))
declare @FolderMedia table (fk_folderID int, fk_media int)

insert into @Folders values (1, 'Folder1', 1000)
insert into @Folders values (2, 'Folder2', 1000)
insert into @Folders values (3, 'Folder1', 2000)
insert into @Folders values (4, 'Folder1', 2000)

insert into @Media values (1, 'graph.jpg', 'image')
insert into @Media values (2, 'timer.jpg', 'image')
insert into @Media values (3, 'timer1.jpg', 'image')
insert into @Media values (4, 'harry_potter.mpeg', 'video')
insert into @Media values (5, 'harry_potter1.mpeg', 'video')
insert into @Media values (6, 'harry_potter2.mpeg', 'video')

insert into @FolderMedia values (1, 1)
insert into @FolderMedia values (1, 3)
insert into @FolderMedia values (1, 6)
insert into @FolderMedia values (2, 2)
insert into @FolderMedia values (2, 4)

select folderName, fk_userID, imageData.imgCount, videoData.videoCount from 
@Folders
left outer join
(
    select fk_folderID, COUNT(*) as imgCount
    from @FolderMedia
    inner join @Media 
    on fk_media = pk_media
    and type = 'image'
    group by fk_folderID
) as imageData
on imageData.fk_folderID = pk_folderID
left outer join
(
    select fk_folderID, COUNT(*) as videoCount
    from @FolderMedia
    inner join @Media 
    on fk_media = pk_media
    and type = 'video'
    group by fk_folderID
) as videoData
on videoData.fk_folderID = pk_folderID
where fk_userID = 1000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文