简体   繁体   English

选择一个大于当前日期的日期

[英]SELECT a date that is greater than current date

I have a MySQL database table where a single cell contains multiple dates and I am wanting to SELECT all rows that contain a cell where one or more of the dates is greater than todays date is this possible? 我有一个MySQL数据库表,其中一个单元格包含多个日期,并且我想选择所有包含一个或多个日期大于今天日期的单元格的行吗? (using PHP) (使用PHP)

Sure: 当然:

select * from MyTable where MyDate > Now()

How does your single field contain multiple dates? 您的单个字段如何包含多个日期? Maybe you should consider a separate table for this instead. 也许您应该为此考虑一个单独的表。

如果该字段是DATEDATETIME ,则可以使用

SELECT * FROM table WHERE datefield > NOW()

A "single cell contains multiple dates" .. is the crux of the problem. “一个单元格包含多个日期” ..是问题的症结所在。 Here are three options. 这是三个选项。

Normalize/fix the schema 规范化/修复模式

Start with an adequately normalized schema - in particular, any column/"cell" can have at most one date (or piece of information). 从充分规范化的模式开始-特别是,任何列/“单元格”最多可以有一个日期(或一条信息)。 This may or may not require introducing another table depending upon the role of the information. 根据信息的作用,这可能需要也可能不需要引入另一个表。

This approach is scalable and works within the RDMBS/RA design and, if done, the other queries presented would "just work". 这种方法是可扩展的,并且可以在RDMBS / RA设计中使用,并且,如果完成,则所提出的其他查询将“有效”。

(This is by far the overall best option and should be done if at all possible. Proper normalization should also guide further schema development to mitigate such issues in the future.) (到目前为止,这是总体上最好的选择,应尽可能进行。正确的规范化还应指导进一步的模式开发,以减轻将来的此类问题。)

Create a normalized view 创建标准化视图

Create a normalized view of the denormalized table (or such a nomalizing query) that can be JOIN ed. 创建可以JOIN的非规范化表的规范化视图(或这种标准化的查询)。 Then use one the various suggested answers (that work on a normalized schema) .. albeit at an extreme disadvantage of not being able to utilize indices. 然后使用各种建议的答案之一(在规范化模式下工作)..尽管无法使用索引是一个极端的缺点。

This is terribly messy, but it can be done. 这非常麻烦,但是可以做到。 See SELECT de-normalized columns into separate records? 看到SELECT取消规范化的列到单独的记录中? for an example. 举个例子 (Note the synthesized row relationship here, although some [non MySQL] databases also support table-valued functions and cross applications of such.) (请注意此处的综合行关系,尽管某些[non MySQL]数据库也支持表值函数和此类的跨应用程序。)

This approach does not scale because it cannot use indices. 该方法无法扩展,因为它无法使用索引。

Filter in client (PHP) 过滤客户端(PHP)

Fetch every row. 获取每一行。 Loop and use explode , etc, as appropriate. 根据需要循环并使用explode等。

This approach does not scale due to lack of indices and requiring all the data is fetched locally. 由于缺少索引, 并且需要在本地获取所有数据,因此该方法无法扩展。


While finding equal values in denormalized columns can be done with various hacks (eg FIND_IN_SET ), finding relative (ie larger/smaller) values is not possible with the same techniques. 虽然可以使用各种技巧(例如FIND_IN_SET )在非规范化的列中找到相等的值,但是使用相同的技术无法找到相对的(即更大/更小)值。 The solution that works in both cases is to fix the underlying schema. 在两种情况下均有效的解决方案是修复基础架构。

如果仅在DATE而不是DATETIME ,则可以使用CURDATE()代替NOW()

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

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