如何计算总日期?

时间:2012-04-12 08:10:00

标签: 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

我列出了此查询最后插入日期的用户

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

此查询正常运行并向我显示此类数据。

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

现在我还想要一个更多的fild来显示特定用户的日期总数..像这样

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

任何人都请告诉我如何调整此查询中的计数.. 我将感激不尽......需要帮助

1 个答案:

答案 0 :(得分:1)

以下是否有效?请试一试:

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,
(
    SELECT count(br3.id)
    from bloodrequest as br3
        where br3.seeker_nic = s.seeker_nic
) as total_dates

FROM seeker as s

虽然,它可能不是最有效的。