![](/img/trans.png)
[英]php mysql Get the latest records based on multiple conditions and count them
[英]how can I get the latest data in mysql with multiple records
我有一張桌子,每張記錄上都有一個日期,我想做的是獲取有關日期的最新信息。 該表具有唯一的ID,該ID等效於另一個表的ID
這是一個例子:
記錄#1的日期為2016/11/11
記錄#2的日期為2016/11/16
當我嘗試使用此代碼時,顯然記錄2是最新數據:
select * from
info join crew_documents_table on info.id = crew_documents_table.document_crew_id
join crew_rank on info.crew_rank = crew_rank.crew_rank_id
where crew_rank in ('1','2','3')
and crew_status = '$crew_status' and vessel = '$vessel_name' group by full_name
獲取最新記錄后,我在這里有一個if statement
,它將回顯最新記錄的日期。
這是我的if statement
代碼
(($row['doc_type'] == '1') ? "$doc_number" : "")
預期的輸出將是:
record # 2 2016/11/16
使用按限制排序和限制。 這將解決您的問題:
select * from
info join crew_documents_table on info.id = crew_documents_table.document_crew_id
join crew_rank on info.crew_rank = crew_rank.crew_rank_id
where crew_rank in ('1','2','3')
and crew_status = '$crew_status' and vessel = '$vessel_name' group by full_name
ORDER BY DATE_FIELD DESC
LIMIT 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.