[英]mysql select last x days between range
This query returns all of the selected values from today's date to 90 days ago: 此查询返回从今天到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
I'd like to find a query that will now select the exact same thing, but only if records DO not exist in the previous query. 我想找到一个查询,该查询现在将选择完全相同的内容,但前提是前一个查询中不存在记录。 EXAMPLE: return records from > 90 days ago, that do not have records in the past 90 days.
示例:返回> 90天前的记录,而这些记录在过去90天内没有记录。
I have tried to do this: (note, 2013-07-03 in the query was 90 days from the first time i ran) 我已尝试执行此操作:(请注意,查询中的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
This does not give me the proper data, I think because i need to somehow exclude records that exist from the past 90 days. 我认为这不能提供适当的数据,因为我需要以某种方式排除过去90天内存在的记录。
THIS IS WHAT IM TRYING TO DO: Select a records with aa value = to 'NP' for the past 90 days, then i need to select records where there is not a np value for greater than 90 days, but these records should be completely unique from the first query (ie the individual could have a case from within the 90 days, and one 180 days ago, i would not need his records.) 这就是我想做的:选择过去90天内aa值=到'NP'的记录,然后我需要选择90天内没有np值的记录,但是这些记录应该完全从第一个查询开始就是唯一的(即个人可以在90天内和180天内收到一个案件,我不需要他的记录。)
EDIT: I forgot to mention I have tried this query with an error near '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
You could use a subquery: 您可以使用子查询:
select * from table where ID NOT IN (select id from table where a=1);
This would essentially select the records from the table that don't match the records the inner query did match. 本质上,这将从表中选择与内部查询匹配的记录不匹配的记录。
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() )
You can use NOT IN statement and enter a SELECT statement that select all the records that should be excluded. 您可以使用NOT IN语句并输入SELECT语句,以选择应排除的所有记录。 A some more info in this thread
此线程中的更多信息
This set should be visits for the same doctor, same cases but a different visit? 这组应该是同一位医生,相同病例但不同的访视?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.