mysql选择范围之间的最后x天

时间:2013-10-03 16:38:32

标签: php mysql

此查询返回从今天的日期到90天前的所有选定值:

 SELECT max(cases_visits.created_dt), users_profiles.account_num,
    concat(cases.patient_first_initial,
    cases.patient_middle_initial, cases.patient_last_initial) AS initials,
    concat(users.first_name, ' ',users.last_name) as name   
    FROM cases
    JOIN users_profiles
    ON users_profiles.user_id=cases.doctor_id
    JOIN cases_visits
    ON cases.id=cases_visits.case_id

    join users on users.id = cases.doctor_id

    WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN     curdate() - INTERVAL 90 DAY AND SYSDATE()

    group by users.first_name

我想找到一个现在将选择完全相同的查询的查询,但仅限于上一个查询中不存在记录的情况。示例:从>返回记录90天前,过去90天内没有记录。

我试过这样做:(注意,2013-07-03在查询中是我第一次跑90天后)

    SELECT cases_visits.created_dt, users_profiles.account_num,
    concat(cases.patient_first_initial,
    cases.patient_middle_initial, cases.patient_last_initial) AS initials,
    concat(users.first_name, ' ',users.last_name) as name
    FROM cases
    JOIN users_profiles
    ON users_profiles.user_id=cases.doctor_id
   JOIN cases_visits
   ON cases.id=cases_visits.case_id
   join users on users.id = cases.doctor_id
   WHERE cases_visits.created_dt < '2013-07-03'
   group by users.first_name

这不会给我正确的数据,我想因为我需要以某种方式排除过去90天内存在的记录。

这就是我要做的事情:在过去90天内选择一个值为'NP'的记录,然后我需要选择np值不超过90天的记录,但是这些记录在第一个查询中应该是完全唯一的(即个人可以在90天内有一个案例,180天前有一个案例,我不需要他的记录。)

编辑:我忘了提及我在'in'附近的错误中尝试了这个查询:

SELECT cases_visits.created_dt, users_profiles.account_num,
concat(cases.patient_first_initial,
cases.patient_middle_initial, cases.patient_last_initial) AS initials,
concat(users.first_name, ' ',users.last_name) as name    
FROM cases 
JOIN users_profiles
ON users_profiles.user_id=cases.doctor_id    
JOIN cases_visits
ON cases.id=cases_visits.case_id    
join users on users.id = cases.doctor_id    
WHERE cases_visits.created_dt < '2013-07-03'
and cases_visits.patient_visit_type = 'NP'    
and not in (select created_dt from cases_visits where  cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE())   
group by users.first_name

3 个答案:

答案 0 :(得分:0)

您可以使用子查询:

select * from table where ID NOT IN (select id from table where a=1);

这实际上会从表中选择与内部查询匹配的记录不匹配的记录。

答案 1 :(得分:0)

SELECT * FROM table WHERE cases_visits.created_dt < '2013-07-03'
AND case_visist.SOME_UNIQUE_ID NOT IN 
(SELECT case_visist.SOME_UNIQUE_ID FROM table WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt BETWEEN     curdate() - INTERVAL 90 DAY AND SYSDATE() )

您可以使用NOT IN语句并输入SELECT语句,以选择应排除的所有记录。还有一些信息in this thread

答案 2 :(得分:0)

这一组应该是针对同一位医生的访问,同样的情况但访问次数不同?

SELECT max(cases_visits.created_dt), users_profiles.account_num,
  concat(cases.patient_first_initial,
  cases.patient_middle_initial, cases.patient_last_initial) AS initials,
  concat(users.first_name, ' ',users.last_name) as name   
FROM cases
JOIN users_profiles
  ON users_profiles.user_id=cases.doctor_id
JOIN cases_visits
  ON cases.id=cases_visits.case_id
JOIN users
  ON users.id = cases.doctor_id
WHERE cases_visits.patient_visit_type = 'NP' && cases_visits.created_dt <= curdate() - INTERVAL 90 DAY
  AND EXISTS(SELECT *
         FROM case_visits AS inside
         WHERE inside.case_id = cases.id
           AND inside.patient_visit_type = 'NP'
           AND inside.created_dt BETWEEN curdate() - INTERVAL 90 DAY AND SYSDATE())
GROUP BY account_num, initials, name