PHP MySQL 日期比较

发布于 2024-12-05 06:42:19 字数 366 浏览 0 评论 0原文

我有一个 Mysql,其中包含行 Data1、data2、data3...Data7,我想与当前日期进行比较,在互联网上搜索到目前为止我得到了这个:

$curDate = date("Y-m-d");
$query = "SELECT Id FROM Programacao where Data1 = $curDate";
$result = mysql_query($query);   
if(!$result) {echo 'Nada';}
while ($row = mysql_fetch_array($result))
{
     echo "Id = ".$row ['Id'];
}

但我只能读取第一个,是否可以比较所有他们同时?

I Have a Mysql with the rows Data1,data2,data3...Data7 and i want to compare with the current date,searching the internet i got this so far:

$curDate = date("Y-m-d");
$query = "SELECT Id FROM Programacao where Data1 = $curDate";
$result = mysql_query($query);   
if(!$result) {echo 'Nada';}
while ($row = mysql_fetch_array($result))
{
     echo "Id = ".$row ['Id'];
}

But i can read only the first one,does it possible to compare all of them at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-12-12 06:42:19

您应该使用逻辑运算符。如果您需要检查数据$ curdate

$query = "
SELECT Id 
FROM Programacao 
WHERE Data1 = $curDate 
   OR Data2 = $curDate
   OR Data3 = $otherDateIfYouNeedOther
";

则应替换 和(如果需要)检查所有这些都可以。

此外,据我记得,您应该在报价中使用日期值,所以正确的是

$query = "
SELECT Id 
FROM Programacao
WHERE Data1 = '$curDate'
   OR Data2 = '$curDate'
   OR Data3 = '$otherDateIfYouNeedOtherOrSameOtherwise'
";

You should use logic operators. If you need check that AT LEAST ONE of Data is $curDate

$query = "
SELECT Id 
FROM Programacao 
WHERE Data1 = $curDate 
   OR Data2 = $curDate
   OR Data3 = $otherDateIfYouNeedOther
";

You should replace or by and if you need to check that ALL OF THEM are OK.

Besides, As far as I remember You should use Date values in quotes, so correct one is

$query = "
SELECT Id 
FROM Programacao
WHERE Data1 = '$curDate'
   OR Data2 = '$curDate'
   OR Data3 = '$otherDateIfYouNeedOtherOrSameOtherwise'
";
青朷 2024-12-12 06:42:19
SELECT Id,CASE 
    WHEN  Data1 = CURDATE() then 'Data1'
    WHEN  Data2 = CURDATE() then 'Data2'
    WHEN  Data3 = CURDATE() then 'Data3'
    ELSE ''
END as Data_Table
FROM Programacao
WHERE Data1 = CURDATE()
   OR Data2 = CURDATE()
   OR Data3 = CURDATE()
....
SELECT Id,CASE 
    WHEN  Data1 = CURDATE() then 'Data1'
    WHEN  Data2 = CURDATE() then 'Data2'
    WHEN  Data3 = CURDATE() then 'Data3'
    ELSE ''
END as Data_Table
FROM Programacao
WHERE Data1 = CURDATE()
   OR Data2 = CURDATE()
   OR Data3 = CURDATE()
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文