我对我的查询感到有点困惑,例如在一个日期范围内获得所有生日。我的情况在这里,
date_from = "2014-12-01" date_to = "2015-01-30"
另一种情况是
date_from = "2015-02-01" date_to = "2015-03-31"
答案 0 :(得分:1)
尝试这样的事情
SELECT * FROM your_table WHERE (date_column BETWEEN '2014-12-01 AND '2015-01-30' ) OR ( date_column BETWEEN '2015-02-01' AND '2015-03-31')
修改强>
SELECT * FROM your_table
WHERE DATE_FORMAT(added_date, '%m-%d') BETWEEN '12-01' AND '12-31'
OR DATE_FORMAT(added_date, '%m-%d') BETWEEN '01-01' AND '12-30'
OR DATE_FORMAT(added_date, '%m-%d') BETWEEN '02-01' AND '03-31'
答案 1 :(得分:0)
尝试此查询
SELECT * FROM table_name WHERE (date>='2014-12-01' AND date<='2015-01-30') OR (date>='2015-02-01' AND date<='2015-03-31')
答案 2 :(得分:0)
尝试
select * from `table` where `birthday` between '2014-12-01' and '2015-01-30'
或
select * from `table` where `birthday` >= '2014-12-01' and `birthday` <= '2015-01-30'
好的,我理解。试试这个
select * from `table` where substr(`birthday`,6) between '12-01' and '01-30'
答案 3 :(得分:0)
试试这个
SELECT * FROM your_table WHERE date_column BETWEEN '2014-12-01' AND '2015-01-30'
union
SELECT * FROM your_table WHERE date_column BETWEEN '2015-02-01' AND '2015-03-31';
答案 4 :(得分:0)
时
select * from `table` where (`birthday` between '2014-12-01' and '2015-01-30') or
(`birthday` >= '2014-12-01' and `birthday` <= '2015-01-30')
答案 5 :(得分:0)
SELECT * FROM table1
WHERE DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "01-01" AND "01-30"
OR DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "12-01" AND "12-31"
OR DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "02-01" AND "03-31"
这样,您的日期中的年份将被忽略,您只需要正确定义范围。 (12月初至12月底,不进入1月;然后从1月开始)
作为在php中构建查询的示例:
$from = "2015-12-01";
$to = "2016-01-31";
if(substr($from, 0, 4) < substr($to, 0, 4)){
$query = 'SELECT * FROM table1
WHERE DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "' . substr($from, 5) . '" AND "12-31"
OR DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "01-01" AND "' . substr($to, 5) . '"';
}else{
$query = 'SELECT * FROM table1
WHERE DATE_FORMAT(`yourField`,"%m-%d") BETWEEN "' . substr($from, 5) . '" AND "' . substr($to, 5) . '"';
}
var_dump($query);
exit;
你需要添加常用的安全方法,并添加对数据有效性的检查。