如何查询id_konsul = 4
的数据,如果相同,我如何从staflow
获取最新的created_at
?
示例
id id_konsul staflow created_at
1 4 1 21/05/2018 11.03
2 4 2 22/05/2018 11.03
3 4 3 23/05/2018 11.03
4 4 4 24/05/2018 11.03
5 4 5 25/05/2018 11.03
6 4 6 26/05/2018 11.03
7 4 7 27/05/2018 11.03
8 4 6 28/05/2018 11.03
9 4 7 29/05/2018 11.03
10 4 7 30/05/2018 11.03
11 4 8 31/05/2018 11.03
比我得到这个
id id_konsul staflow created_at
1 4 1 21/05/2018 11.03
2 4 2 22/05/2018 11.03
3 4 3 23/05/2018 11.03
4 4 4 24/05/2018 11.03
5 4 5 25/05/2018 11.03
8 4 6 28/05/2018 11.03
10 4 7 30/05/2018 11.03
11 4 8 31/05/2018 11.03
答案 0 :(得分:0)
您可以使用INNER JOIN。
示例:强>
SELECT t0.* from {TABLE_NAME} AS t0 INNER JOIN {TABLE_NAME} t1 ON t0.staflow = t1.staflow WHERE t0.created_at > t1.created_at and t0.id_konsul = 4
答案 1 :(得分:0)
请尝试此查询。
SELECT * FROM table_name WHERE id_konsul = 4 ORDER BY created_at DESC LIMIT 1;
答案 2 :(得分:0)
要获取每个staflow属性的最新行,您可以使用自联接
private Response mapException(Throwable originalThrowable) throws Throwable {
if (throwable instanceof WebApplicationException) {
WebApplicationException webApplicationException = (WebApplicationException)throwable;
}
this.processingContext.routingContext().setMappedThrowable(throwable);
waeResponse = webApplicationException.getResponse();
if (waeResponse.hasEntity()) {
LOGGER.log(java.util.logging.Level.FINE, LocalizationMessages.EXCEPTION_MAPPING_WAE_ENTITY(waeResponse.getStatus()), throwable);
return waeResponse;
}
long timestamp = this.tracingLogger.timestamp(ServerTraceEvent.EXCEPTION_MAPPING);
ExceptionMapper mapper = this.runtime.exceptionMappers.findMapping(throwable);
}
或使用左连接
select a.*
from demo a
join (
select id_konsul,staflow, max(created_at) created_at
from demo
where id_konsul = 4
group by id_konsul, staflow
) b on a.staflow = b.staflow
and a.id_konsul = b.id_konsul
and a.created_at = b.created_at;
要使用laravel的查询构建器编写上述查询,您可以使用以下引用
Laravel Eloquent select all rows with max created_at
答案 3 :(得分:0)
尝试此查询
SELECT * FROM test1 n
WHERE created_at=(SELECT MAX(created_at)FROM test1
WHERE staflow=n.staflow)
order by id