gpt4 book ai didi

php - MySQL比较日期不返回结果

转载 作者:行者123 更新时间:2023-11-29 03:23:22 25 4
gpt4 key购买 nike

我需要一个帮助。我无法使用 PHP 和 MySQL 根据某些条件从表中获取值。我在下面解释我的表格。

db_subcat:

id    subcat_id   sub_name      from_date       to_date
------------------------------------------------------------
1 60 Ram 2016-10-25 2016-10-28
2 60 Ram
3 61 Raj

这里有些行没有 from_date 和 to_date 值。我在下面解释我的查询。

$date="2016-10-23";
$sql="select * from db_subcat
where from_date <='".$date."' and to_date >= '".$date."' and from_date !='' and to_date !=''
group by subcat_id
union all select * from db_basic
where from_date ='' and to_date =''
group by sucat_id";

我的问题是当表没有(from_date ='' and to_date ='')(from_date !='' and to_date != '')。这里我需要使用以下所有条件获取值。

1- 如果 from_date 和 to_date 有值,如果值存在则匹配。

2- 如果 from_date 和 to_date 有值或两个条件值都为空,则应该获取。

3- 如果表没有 (from_date ='' and to_date ='')(from_date !='' and to_date !='') 条目.

请帮我解决这个问题。

最佳答案

如果我没理解错的话,你可能需要使用 ISNULL 并且你可以结合这两个查询:

select * from db_subcat
where ((from_date IS NULL OR from_date ='') AND (to_date IS NULL OR to_date =''))
OR (from_date <='".$date."' and to_date >= '".$date."')
group by subcat_id

请注意,此GROUP BY 子句在除MySQL 以外的所有DBMS 中均无效。您不应该使用 SELECT * 语句来执行此操作,始终指定列,并且每一列都应该位于 GROUP BY 子句中或包含一个聚合函数。

关于php - MySQL比较日期不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40254909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com