SQL 查询从另一个 SQL 查询中选择 COUNT
我在进行 SQL SERVER 2000 查询时遇到了一些问题。这是我的场景:
我有一个名为 Folders 的表,其中包含 3 列:pk_folderID、folderName 和 fk_userID >。
此外,我还有另一个名为 FolderMedia 的表,它存储属于某个文件夹的媒体(无论什么)。有 2 列:fk_folderID、fk_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上是这样的:
更新(基于附加请求):
要将某种
TOP 1 Media.Name
包含到结果集中,可以将上面的查询更改为this:如果最小
FolderMedia.timestamp
值在其文件夹内不唯一,则相应Media.Name
的最终值将由其字母顺序排序决定。特别是,上面的查询选择了集合中的最后一个(使用MAX()
)。Basically something like this:
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:In cases where minimal
FolderMedia.timestamp
values are not unique within their folders, the ultimate value of the correspondingMedia.Name
will be decided by its alphabetical sorting. In particular, the above query selects the last one of the set (withMAX()
).从
Folders
表中获取所需的所有数据,将其与FolderMedia
和Media
左侧连接使用
sum
和case
来计算所有视频和图像的数量。SUM(CASE WHEN mediaTypeId = videoId THEN 1 ELSE 0 END) as videoCount
Get all data you need from
Folders
table left join it withFolderMedia
andMedia
use
sum
withcase
inside to count all videos and images.SUM(CASE WHEN mediaTypeId = videoId THEN 1 ELSE 0 END) as videoCount
//选择文件夹名称,计数(当Folders.pk_folderID为1时,否则为空结束)
等..
//select folderName ,count(case when Folders.pk_folderID then 1 else null end)
etc..
这是带有一些示例数据的查询。希望有帮助。
Here is a query with some example data. Hope it helps.