我该如何回答这个SQL问题? [英] How do I answer this SQL question?

查看:57
本文介绍了我该如何回答这个SQL问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我需要做什么,所以我想我会请一些建议。



问题:

选择胜利年份的马,4岁时体重最轻。



问我要问的是从一张关于马使用的表格来自多场赛马的数据,你需要选择马,显示胜利的年份,但这匹马必须拥有所有4岁马匹中最低的重量。



到目前为止我得到了什么:

SELECT Horse

来自MelCup

WHERE年龄如'%Min(4)%'

AND年



请帮助!



恢复:解决方案没有问题,没有任何意义! - OriginalGriff [/ edit]



我的尝试:



选择马

来自MelCup

WHERE年龄如'%Min(4)%'

AND年

解决方案

首先查看你要返回的内容:

  SELECT 马,WinYear  FROM  MelCup  WHERE 年龄> =  4  

这样可以让你获得四年或四年以上的所有马匹,赢得比赛的胜利,以及他们赢得的那一年。

但是那匹马和年份相当多! br />
因此,您需要按权重递增对订单进行排序。这很简单 - 只需添加一个ORDER BY子句来指定排序顺序:

  SELECT  Horse,WinYear < span class =code-keyword> FROM  MelCup  WHERE 年龄> =  4  < span class =code-keyword> ORDER   BY 重量 ASC   
我们也可以处理它,告诉SQL我们只对第一个结果感兴趣:

  SELECT   TOP   1  Horse,WinYear  FROM  MelCup  WHERE 年龄> = < span class =code-digit> 4   ORDER   BY 重量 ASC  



显然,您可能需要更改列名等,但这是一般的想法。


I’m unsure of what I need to do so I thought I would ask for some advice.

The question:
Select the horse, with year of win, having the lightest weight for a 4 year old.

What the question is asking me to do is from a table about horses using data from multiple horse races, you need to select the horse, showing the year of the win but this horse must have the lowest weight out of all the 4 year old horses.

What I’ve got so far:
SELECT Horse
FROM MelCup
WHERE Age like '%Min(4)%'
AND Year

Please help!

[edit]Reverted: solution makes no real sense without the question! - OriginalGriff[/edit]

What I have tried:

SELECT Horse
FROM MelCup
WHERE Age like '%Min(4)%'
AND Year

解决方案

Start by looking at what you have to return:

SELECT Horse, WinYear FROM MelCup WHERE Age >= 4

That gives you all the horses four years or over, who won the race, and the year they won.
But that's quite a lot of horses and years!
So you need to sort those into an order, by ascending weight. That's easy - just add an ORDER BY clause to specify the sort order:

SELECT Horse, WinYear FROM MelCup WHERE Age >= 4 ORDER BY Weight ASC

That still gives you all the horses, but at least the one you want is at the top!
We can deal with that as well, by telling SQL we are only interested in the first result:

SELECT TOP 1 Horse, WinYear FROM MelCup WHERE Age >= 4 ORDER BY Weight ASC


Obviously, you may need to change your column names and suchlike, but that's the general idea.


这篇关于我该如何回答这个SQL问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆