简体   繁体   English

按日期显示每人一个结果

[英]show one result per person sorted by date

in my local website, people can pass a test, and administrator can see the result. 在我本地的网站上,人们可以通过测试,管理员可以看到结果。 before, all the result were display but it was ugly, so I decided to show only the last test people pass. 以前,所有结果都可以显示,但是很难看,所以我决定只显示最后一次通过测试的人。 but I'm stuck because I can show only one result per person, but I can't sort him by date :( 但我被困住了,因为每个人只能显示一个结果,但是我无法按日期对他进行排序:(

SELECT * FROM resultateval join personne using (id) group by id

this is the query who show one result per people and here is the table. 这是显示每人一个结果的查询,这是表。

在此处输入图片说明

it's supposed to show 它应该显示

654321 / 08-06-2018 / 12 : 02 / 5 / 8 / 1
 A256589 / 05-06-2018 /13 : 05 / 7 / 10 / 2

Thank you! 谢谢!

Because you stored time and date separately, the query you'll need to do this is relatively ugly. 由于您分别存储时间和日期,因此您需要执行的查询相对较丑。 We can form an effective timestamp by using ADDTIME to add the heuereeval time to the dateeval date. 我们可以通过使用ADDTIMEheuereeval时间添加到dateeval日期来形成有效的时间戳。 Then, this is just a basic aggregation join query: 然后,这只是一个基本的聚合联接查询:

SELECT t1.*
FROM resultateval t1
INNER JOIN
(
    SELECT id, MAX(ADDTIME(dateeval, heureeval)) AS max_date
    FROM resultateval
    GROUP BY id
) t2
    ON t1.id = t2.id AND
       ADDTIME(t1.dateeval, t1.heureeval) = t2.max_date;

在此处输入图片说明

Demo 演示

For future reference, avoid storing the date and time separately, unless there is a very good reason for doing so (and I don't see one here). 为了将来参考,请避免分别存储日期和时间,除非有很好的理由(我在这里看不到)。

Edit: As a I feared, based on comments it appears that you have stored your dates as text. 编辑:正如我所担心的,基于注释,看来您已将日期存储为文本。 You can use the following function call to generate a date based on the date's text: 您可以使用以下函数调用根据日期文本生成日期:

STR_TO_DATE(dateeval, '%d-%m-%Y')

Just replace in my original query dateeval with the above call to STR_TO_DATE and it should still work. 只需将我的原始查询dateeval替换为上面对STR_TO_DATE调用,它仍然可以正常工作。

What you are doing is very wrong. 你在做什么是非常错误的。 You group by id , but you select all columns. 您按id分组,但选择了所有列。 As id is not unique in your joined data, this makes no sense and is invalid SQL. 由于id在联接的数据中不是唯一的,所以这没有意义,并且是无效的SQL。 You cannot say: "Give me the dateeval (etc.) for the id ". 您不能说:“请给我id dateeval (例如)”。 You must say something like: " "Give me the minimum / maximum / average dateeval for the id ". 您必须说出类似以下内容:“”给我id 的最小/最大/平均 dateeval

This query should result in an error, but MySQL outside ONLY_FULL_GROUP_BY mode let's this slip and silently converts select * to select id, any_value(dateeval), any_value(heureeval) ... . 这个查询应该导致一个错误,但是在ONLY_FULL_GROUP_BY模式之外的MySQL让我们滑过,并悄悄地转换select *select id, any_value(dateeval), any_value(heureeval) ... So you get arbitrarily picked values that can even stem from different records. 因此,您可以随意选择甚至来自不同记录的值。

What you want is something like this: 您想要的是这样的:

select *
from resultateval 
join personne using (id)
where (id, timestamp(dateeval,heureeval)) in
(
  select id, max(timestamp(dateeval,heureeval)) 
  from resultateval 
  join personne using (id)
  group by id
);

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

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