【问题标题】:What is the best way to display the details of the highest value显示最高价值细节的最佳方式是什么
【发布时间】:2022-01-26 00:00:58
【问题描述】:

你好,我想知道什么是正确的方式,我被要求提供关于最年长的艺术家的信息。

FROM ListOfArtists
WHERE (DateDeceased - DateofBirth) = (SELECT 
            MAX(DateDeceased - DateofBirth)
        FROM ListOfArtists);

或者:

SELECT *
FROM ListOfArtists
ORDER BY (DateDeceased - DateofBirth) DESC
LIMIT 1

提前致谢!

【问题讨论】:

  • 我想知道什么是正确的方法那么当你尝试时,哪一种能给你预期的结果呢?
  • 预期的结果是像这样只介绍最年长的艺术家:ArtistID LastName FirstName Nationality DateDeceased DateOfBirth 5 Chagall Marc French 1985 1887
  • 他们做了两件不同的事情;一个只报告 1 个结果,一个报告所有价值最高的艺术家。你要哪个?但那是 not 如何在 mysql 中获得两个日期之间的差异;这些只是几年吗?
  • 他们都给了我同样的结果,最年长的艺术家
  • 它们都给你相同的结果,因为你碰巧有一个特定年龄的艺术家。但是,如果您有两位活了 98 岁并且代表最长寿命的艺术家,您会从这两个查询中得到两个不同的结果。

标签: mysql sql


【解决方案1】:

如果我完全过度考虑这一点,我深表歉意,但我忍不住指出这一点。

您的第一条评论表明您只存储出生和死亡年份 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
1 Botticelli Sandro Italian 1510 1445
2 Da Vinci Leonardo Italian 1519 1452
3 Buonarroti Michelangelo Italian 1564 1475
4 Bonestell Chesley American 1986 1888
5 Chagall Marc French 1985 1887

您的第一个查询 -

SELECT *
FROM ListOfArtists
WHERE (DateDeceased - DateofBirth) = (SELECT 
            MAX(DateDeceased - DateofBirth)
        FROM ListOfArtists);

返回 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
4 Bonestell Chesley American 1986 1888
5 Chagall Marc French 1985 1887

您的第二个查询 -

SELECT *
FROM ListOfArtists
ORDER BY (DateDeceased - DateofBirth) DESC
LIMIT 1

返回 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
5 Chagall Marc French 1985 1887

现在考虑以下版本的表格,但存储完整的出生和死亡日期 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
1 Botticelli Sandro Italian 1510-05-17 1445-01-01
2 Da Vinci Leonardo Italian 1519-05-02 1452-04-15
3 Buonarroti Michelangelo Italian 1564-02-18 1475-03-06
4 Bonestell Chesley American 1986-06-11 1888-01-01
5 Chagall Marc French 1985-03-28 1887-07-07

您的第一个查询返回 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
4 Bonestell Chesley American 1986-06-11 1888-01-01

您的第二个查询返回 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
4 Bonestell Chesley American 1986-06-11 1888-01-01

更好的查询是 -

SELECT *
FROM ListOfArtists
ORDER BY DATEDIFF(DateDeceased, DateofBirth) DESC
LIMIT 1;

返回 -

ArtistID LastName FirstName Nationality DateDeceased DateOfBirth
4 Bonestell Chesley American 1986-06-11 1888-01-01

下面的查询说明了两者的区别——

SELECT *, YEAR(DateDeceased) YearDied, YEAR(DateofBirth) YearBorn, (DateDeceased - DateofBirth), DATEDIFF(DateDeceased, DateofBirth) DaysOld, FLOOR(DATEDIFF(DateDeceased, DateofBirth) / 365.25) YearsOld
FROM ListOfArtists;
ArtistID LastName FirstName Nationality DateDeceased DateOfBirth YearDied YearBorn (DateDeceased - DateofBirth) DaysOld YearsOld
1 Botticelli Sandro Italian 1510-05-17 1445-01-01 1510 1445 650416 23876 65
2 Da Vinci Leonardo Italian 1519-05-02 1452-04-15 1519 1452 670087 24487 67
3 Buonarroti Michelangelo Italian 1564-02-18 1475-03-06 1564 1475 889912 32490 88
4 Bonestell Chesley American 1986-06-11 1888-01-01 1986 1888 980510 35955 98
5 Chagall Marc French 1985-03-28 1887-07-07 1985 1887 979621 35693 97

这是SQL Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2020-11-05
    • 2011-03-02
    • 1970-01-01
    • 2020-05-04
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多