問題描述
我有 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 個不同的值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!