mysql查询获得2个日期之间的第1个特定日期

时间:2016-08-14 04:35:57

标签: php mysql

如何在2个日期之间获得第一个星期天?     我在myTable中只有两个字段(dt_from和dt_to)

dt_from = '2016-08-6';
dt_to = '2016-08-19';

SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;

3 个答案:

答案 0 :(得分:2)

如何在约会之后立即获得第一个星期日?

SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;

http://sqlfiddle.com/#!9/7fce5

如果星期日不在日期之间:

SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;

http://sqlfiddle.com/#!9/83d0f0/4

稍微短一些:

SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;

http://sqlfiddle.com/#!9/6adab3/1

答案 1 :(得分:0)

试试这个:

ProjectModule->Build Phases->Copy Bundle Resources

答案 2 :(得分:0)

假设您只需要起诉数据库来获取日期范围,这样您就不需要任何查询之王。

<?php

echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));

您可以添加一些验证以确保在结束日期之前