简体   繁体   English

我不知道的SQL查询

[英]SQL Query I can't figure out

I have the tables: 我有桌子:

contest_images {contestID, imageID}
image {ID, userID}
user {ID}

(They contain more information than that, but this is all we need) (它们包含的信息更多,但这只是我们所需要的)

My query so far: 到目前为止我的查询:

SELECT imageID, userID FROM contest_images
JOIN image ON contest_images.imageID = image.ID 
JOIN user ON user.ID = image.userID
ORDER BY contest_images.imageID  DESC

The contest_images can contain multiple images from one user (which is intended) Competition_images可以包含一个用户的多个图像(有意使用)

I want to retrieve the x newest all users have added. 我想检索所有用户已添加的x个最新消息。 (So I can restrict the users to only have one image in the contest at a time) (因此,我可以限制用户一次只能在比赛中获得一张图片)

I also tried to make a view displaying {contestID, imageID, userID}. 我还尝试制作一个显示{contestID,imageID,userID}的视图。

Thanks in advance 提前致谢

Try like this, 这样尝试

SELECT MAX(imageID), userID FROM contest_images
JOIN image ON contest_images.imageID = image.ID 
JOIN user ON user.ID = image.userID
GROUP BY image.userID

To limit to one image/ latest image in contest at a time you can use Top operator as : 要一次将一张图片/最新图片限制在比赛中,您可以将Top运算符用作:

SELECT top 1 imageID, userID FROM contest_images
JOIN [image] ON contest_images.imageID = [image].ID 
JOIN [user] ON [user].ID = image.userID
ORDER BY contest_images.imageID  DESC 

Thank you guys very much! 非常感谢你们! :) :)

I went with 我去了

SELECT contestID, MAX(imageID), userID FROM contest_images
JOIN image ON contest_images.imageID = image.ID 
JOIN user ON user.ID = image.userID
GROUP BY image.userID

Hence the contestID will be specified in a WHERE clause in the end :) 因此,比赛ID将在最后的WHERE子句中指定:)

But thanks alot both of you :P (I really was stuck on this.. :( ) 但是,非常感谢你们两个:P(我真的对此很执迷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM