简体   繁体   English

mysql选择datetime字段小于指定值的所有记录

[英]mysql select all records where a datetime field is less than a specified value

So I am trying to run a query that will select, in this case, clients that haven't had an appointment is X amount of time (2 weeks for instance). 所以我试图运行一个将选择的查询,在这种情况下,没有预约的客户是X时间(例如2周)。 It boils down to "show me a list of clients that haven't had an appointment in 2 weeks". 它归结为“向我显示未在2周内预约的客户名单”。 I'm attempting to solve this in php by doing something like: 我试图通过做类似的事情来解决这个问题:

$date = new DateTime;
$ago = new DateInterval('P2W');
$target = $date->sub($ago);
//query to select clients that aren't scheduled after the $target date
$clients = ...;

Two tables involved, appt_tbl , and clients_tbl . 涉及两个表, appt_tblclients_tbl appt_tbl stores a client_id for each appointment record. appt_tbl为每个约会记录存储client_id。

So essentially what I need is to select the "max" appointment for each client, and if it's < my $target date, include them in the query results. 基本上我需要的是为每个客户选择“最大”约会,如果它是<我的$目标日期,请将它们包含在查询结果中。 I've tried various flavors of queries, queries with sub queries, but I'm stumbling on getting this query right. 我尝试了各种各样的查询,查询子查询,但我很难找到正确的查询。

My current attempt looks something like: 我目前的尝试看起来像:

SELECT * 
FROM clients_tbl 
INNER JOIN
(
    SELECT client_id 
    FROM appt_tbl 
    WHERE MAX(appt_date_time) < '2012-07-22' 
    GROUP BY client_id
) appts ON appts.client_id = clients_tbl.client_id;

This should also include clients that have never been scheduled (IE won't appear in the appt_tbl), but not clients who have an appointment booked in the next two weeks. 这还应该包括从未安排过的客户(IE不会出现在appt_tbl中),但不包括在接下来的两周内预约的客户。

SELECT a.*
FROM clients_tbl a
LEFT JOIN appt_tbl b ON 
          a.client_id = b.client_id AND 
          b.appt_date_time >= CURDATE() - INTERVAL 2 WEEK 
WHERE b.client_id IS NULL

What this query does first (before the WHERE filtering) is select all clients whether or not they have a scheduled appointment greater than two weeks ago. 此查询首先执行的操作(在WHERE过滤之前)选择所有客户端,无论他们是否具有超过两周前的计划约会。

If the client does not have an appointment greater than two weeks ago, the values in the joined table will be NULL . 如果客户端没有超过两周的约会,则联接表中的值将为NULL We want all rows where the join conditions did not satisfy (ie values in joined table are null), which is done with WHERE b.client_id IS NULL . 我们希望连接条件不满足的所有行(即连接表中的值为null),这是通过WHERE b.client_id IS NULL

This also includes into the result-set clients who do not have any corresponding appointments at all. 这也包括完全没有任何相应约会的结果集客户。

Clients who have appointments in the future are excluded. 未来有约会的客户将被排除在外。

There is also no need to construct a datetime string in PHP. 也没有必要在PHP中构造日期时间字符串。 You can simply do it straight in the query (although you may pass in the number of weeks ago as a parameter). 您可以直接在查询中执行此操作(尽管您可以将周数作为参数传递)。

This could be simple enough i think. 我认为这可能很简单。

select users from time_sheet where created_date >= date('2013-12-01');

In your case you got to do like this. 在你的情况下,你必须这样做。 Instead of this 而不是这个

WHERE MAX(appt_date_time) < '2012-07-22' 

do this 做这个

WHERE MAX(appt_date_time) < date('2012-07-22')

That acually makes the date comparision, earlier one was date with string comparision. 这实际上使日期比较,早期的一个是字符串比较的日期。

Cheers!! 干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM