How can I select 4 distinct values from 2 tables containing many rows of data in SQL Server?(如何从 SQL Server 中包含多行数据的 2 个表中选择 4 个不同的值?)
问题描述
我有 2 个表,我试图从 GalleryID DESC 的排序中选择 4 个结果.我希望返回的字段是 Gallery 表中的 GalleryID、GalleryTitle、GalleryDate 和 Media 表中的 MediaThumb.
I have 2 tables that Im trying to select 4 results from ordered by GalleryID DESC. The fields Im looking to return are GalleryID, GalleryTitle, GalleryDate from the Galleries table, and MediaThumb from the Media Table.
现在问题是 Media 表具有 GalleryID 的 10 的倍数.因此,Media 表中可能有 10 行具有相同的 GalleryID.我所需要的只是一个 MediaThumb 来配合galleryid.
Now the catch is that the Media table has multiples of 10 of GalleryID's. So, there could be 10 rows in the Media table with the same GalleryID. All I need is one MediaThumb to go along with the galleryid.
我不确定如何制定查询以从画廊表中返回 4 个不同的画廊 ID,并从媒体表中返回一个 MediaThumb.
Im not sure how to formulate a query to return 4 distinct galleryid's from the galleries table and a MediaThumb from the media table.
基本上,我想要做的是返回 4 个独特的照片画廊,其中包含一张来自媒体表的封面照片.
Essentially what im trying to do is return 4 unique photo galleries with a cover photo from the media table.
任何帮助将不胜感激.我通过 sqlserver 中的查询设计器提供了我的 2 个表的快照.
Any help would be appreciated. I provided a snapshot via query designer in sqlserver of my 2 tables.
推荐答案
类似的事情?
SELECT TOP 4
g.GalleryID
,g.GalleryTitle
,g.GalleryDate
,MAX(m.MediaThumb) AS MaxMediaThumb
FROM Galleries g
INNER JOIN Media m
ON g.GalleryID = m.GalleryID
GROUP BY g.GalleryID, g.GalleryTitle, g.GalleryDate
这篇关于如何从 SQL Server 中包含多行数据的 2 个表中选择 4 个不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 SQL Server 中包含多行数据的 2 个表中选择
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
