简体   繁体   English

选择两个日期字段之间某个月份的MySQL行

[英]Select MySQL rows where a certain month is between two date fields

I have a page which displays events by each month. 我有一个页面,按月显示事件。 Each event has a start_date and finish_date as yymmdd. 每个事件都有一个start_date和finish_date作为yymmdd。 If an event spans over two different months it should show in each month it spans over. 如果某个事件跨越两个不同的月份,则应在每个月份显示一次。 For example, if I have an event with a start date of 03/19/2014 and a finish date of 06/19/2014 it should appear in the months of March, April, May and June. 例如,如果我有一个开始日期为2014年3月19日,结束日期为2014年6月19日的事件,则该事件应显示在3月,4月,5月和6月。

I've been trying to do this in wordpress with meta_query but I just don't think it's possible: 我一直在尝试使用meta_query在wordpress中执行此操作,但我认为这是不可能的:

        // Get chosen month to display from URL
$events_month = sanitize_text_field($_GET["month"]);
$events_year = sanitize_text_field($_GET["year"]);

// Convert chosen month to display to a timestamp
$ts = strtotime("$events_month $events_year");

// Create chosen month start end end dates to use for query
$month_start_date = date('Ym01', $ts);
$month_end_date = date('Ymt', $ts);

$args = array(
    'post_type' => 'events',
    'posts_per_page' => 50,
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'meta_key' => 'start_date',
    'meta_query'        => array(
        array(
            'key' => 'start_date',
            'value' => array($month_start_date, $month_end_date),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);

$events = new WP_Query($args);

So'm I'm looking for a pure MySQL solution. 因此,我正在寻找一种纯MySQL解决方案。 Any help please? 有什么帮助吗?

I can't tell you much about WP meta queries , but this WHERE clause in SQL should cover all four types of event durations: 关于WP meta queries ,我无法告诉您太多信息,但是SQL中的WHERE子句应涵盖事件持续时间的所有四种类型:
Events that happen 发生的事件

  • start in the current month, 从当月开始
  • end in the current month, 在当月结束
  • only in the current month (implies previous conditions), 仅在当前月份(暗示以前的条件),
  • span over the whole current month 跨越整个当月

Query: 查询:

SELECT ... FROM ... WHERE
    start_date BETWEEN month_start AND month_end
    OR end_date BETWEEN month_start AND month_end
    OR month_start BETWEEN start_date AND end_date


EDIT : 编辑:
Actually, a much easier solution is to select events that 实际上,一个更简单的解决方案是选择事件

  • start before the current month or within it 在当月之前或之内开始
  • AND end within this month or after it AND在本月或之后结束

Query; 查询;

 SELECT ... FROM ... WHERE WHERE start_date <= month_end AND end_date >= month_start 

Without knowledge of meta query stuff (my reference was this ), what you want should work like this: 在不了解元查询内容的情况下(我的参考是this ),您想要的应该像这样工作:

 'meta_query' => array( 'relation' => 'AND', // default is AND array( 'key' => 'start_date', 'value' => $month_end_date, 'type' => 'numeric', 'compare' => '<=' ), array( 'key' => 'end_date', 'value' => $month_start_date, 'type' => 'numeric', 'compare' => '>=' ) ), 

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

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