0

我有两张桌子,我想得到最后输入的日期。

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

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

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

任何人都请告诉我如何在这个查询中调整计数..我会很感激.... plzz需要帮助

4

1 回答 1

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

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

于 2012-04-12T08:18:33.530 回答