簡體   English   中英

無法在列出用戶php MYSQL中獲得最大日期

[英]cant get maximum date in listing users php MYSQL

我有兩個表,我想獲取最后輸入的日期。

The first table is seeker:

seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza

The second one is requestblood:

id-------seeker_nic-----requireddate
1-------  111 ----------2012/10/9
2 ------- 222-----------2012/5/8
3 ------  111-----------2012/12/12
4 ------- 111-----------2012/11/12
5---------111-----------2012/09/09
6 ------- 222-----------2012/7/9
7 ------- 333 ----------2012/4/4

現在,我要列出用戶的上次插入日期,例如:

s.no---- username----- requireddate
 1------- ali---------- 2012/09/09
 2------- umer--------- 2012/7/9
 3------- raza--------- 2012/4/4

我正在使用此查詢

SELECT seeker.username,bloodrequest.requireddate, 
 max( bloodrequest.bloodrequest_id ) AS maxdate
FROM seeker
JOIN bloodrequest ON seeker.seeker_nic = bloodrequest.seeker_nic
GROUP BY seeker.username

當我在phpmyadmin中運行此查詢時。它列出所有用戶並顯示每個用戶的第一個插入日期..但是我想要最后一個插入日期...我該怎么辦..請我需要幫助:(

編輯真實答案:

不是很干凈的sql,但是它將為您提供所需的結果。 我敢肯定有一種更好的分組方式,但是這行得通。 :)

SELECT s.username,
(
    SELECT br1.requireddate 
    from bloodrequest as br1 
    where br1.bloodrequest_id = 
    (
        select max(br2.bloodrequest_id)
        from bloodrequest as br2 
        where br2.seeker_nic = s.seeker_nic
    )
) as requireddate
FROM seeker as s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM