0

假设我有一张比赛结果表。该表由七列组成,如下所示:日期(MySql 日期格式为 xxxx-xx-xx ),以及一列分别为前六名选手的姓名,分别为 First、Second、Third、Fourth、Fifth 和 Sixth。我有几组结果,在各种整理器列中可能有 100 个左右不同的名称。我需要一个查询,允许我列出姓名出现在任何终结者列( First、Second、Third、Fourth、Fifth、Sixth )中的每个人,以及他们姓名出现的最近日期。我不需要基于完成位置的单独结果,因此我需要将所有六个完成器列集中在一起。大多数名字会出现在几十个不同的日期,但我只需要每个名字出现的最近日期。理想情况下,结果将生成每个名称及其最近完成日期的列表,从最近到最近排序。我试图创建一个小提琴来证明这一点,但无论出于何种原因,我都无法让日期在小提琴中正常工作。无论如何,任何可以为此提供哪怕一点帮助的人都将不胜感激。

4

3 回答 3

0

这给了我我需要的结果。当我查询所有六列时,我基本上只是使用“名称”来捕捉完成者,并使用“MAX(日期)作为最后一个”来选择每个“名称”的最新显示。

SELECT name, MAX(Date) as last
FROM (
SELECT first name,Date FROM Results
UNION ALL
SELECT second, Date FROM Results
UNION ALL
SELECT third, Date FROM Results 
UNION ALL
SELECT fourth, Date FROM Results
UNION ALL
SELECT fifth, Date FROM Results
UNION ALL
SELECT sixth, Date FROM Results) Q 
GROUP BY name 
ORDER BY last ASC;
于 2013-05-16T01:44:44.037 回答
0

select t.first, a.date from (select distinct(first) from race union select distinct(second) from race union select distinct(third) from race union select distinct(fourth) from race union select distinct(fifth) from race union select distinct(sixth) from race) as t, race a where t.first in (a.first, a.second, a.third, a.fourth, a.fifth, a.sixth)

于 2013-05-16T08:14:57.370 回答
0
SELECT <table>.date, <table>.first as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.second as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.third as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.fourth as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.fifth as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.sixth as name FROM <table> GROUP BY name
ORDER BY date
于 2013-05-15T22:50:33.423 回答