查询以检索Laravel 5.2历史记录表中的最新条目 [英] Query to retrieve the latest entry in a history table in Laravel 5.2

查看:105
本文介绍了查询以检索Laravel 5.2历史记录表中的最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是桌子

表:StudentHistory

table: StudentHistory

id   |  date     | name  | grade  | subject
---- | ------    |------ |--------|------
1    | 5/1/2017  |Mark   |   a    |   science
2    | 7/1/2016  |Earl   |   c    |   english
3    | 2/1/2015  |John   |   a    |   english
4    | 6/1/2016  |Mike   |   c    |   science
5    | 4/1/2016  |Matt   |   e    |   english
6    | 2/1/2017  |Mark   |   d    |   science
7    | 3/1/2016  |Earl   |   a    |   english
8    | 7/1/2015  |John   |   d    |   english
9    | 8/1/2016  |Mike   |   c    |   science

我想做的是,仅对有成绩的学生用英语填写最新成绩.它应该像这样显示

What I want to happen is to populate the latest grades ONLY in English for students who have one. It should show like this

7/1/2016 Earl c
7/1/2015 John d
4/1/2016 Matt e

我知道了,但是没有给出最新的

I got this but it doesn't give the latest based on

$englishgrades = StudentHistory::('date', 'name', 'grade') ->where('subject', 'english') ->groupBy('name') ->get();

$englishgrades = StudentHistory::('date', 'name', 'grade') ->where('subject', 'english') ->groupBy('name') ->get();

请帮助

推荐答案

您可以执行降序并接收第一行,该行将成为数据库中的最后一行.但问题是您以错误的方式存储日期.如@Strawberry所提到的,它的日期或月份不明确.我建议您使用 mysql datetime 选项,然后只需执行以下代码即可.在这种情况下,您可能必须以适当的方式解析每个日期

You can do descending order and receive first row which is going to be your last row in database. But the thing is you store date in a wrong way. as mentioned by @Strawberry its not clear date or month. I would suggest you to do use mysql datetime option then you can simply do the code I post below. In this case you might have to parse each date for a proper way

$englishgrades = StudentHistory::select('date', 'name', 'grade')
->where('subject', 'english')
->groupBy('name')
->orderBy('date','desc')
->get();

这篇关于查询以检索Laravel 5.2历史记录表中的最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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