【问题标题】:Datediff from 2 columns来自 2 列的日期差异
【发布时间】:2019-06-14 21:53:21
【问题描述】:

我正在尝试获取两个日期之间的天数,但我只收到错误消息。我尝试搜索它,但没有任何东西对我有帮助,我希望你们能在这里帮助一个男孩。

我使用此代码得到的错误:

致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或 访问冲突:1582 对本机的调用中的参数计数不正确 函数 'DATEDIFF' 在 C:\xampp\htdocs\Rent-a-Car\pages\medewerkers.php:197 堆栈跟踪:#0 C:\xampp\htdocs\Rent-a-Car\pages\medewerkers.php(197): PDO->query('SELECT DATEDIFF...') #1 {main} 抛出 C:\xampp\htdocs\Rent-a-Car\pages\medewerkers.php 在第 197 行

my code to display it several times in the table

The database

使用的sql命令:

$sql1 = "SELECT * FROM factuur 
LEFT JOIN factuurregel ON factuur.Factuurnummer = factuurregel.Factuurnummer
LEFT JOIN gebruiker ON factuur.Klantcode = gebruiker.Klantcode
LEFT JOIN auto ON factuur.Kenteken = auto.Kenteken";
$sql2 = "SELECT DATEDIFF (day, Begindatum, Einddatum) AS Tijd from factuurregel;";

【问题讨论】:

标签: php mysql sql date


【解决方案1】:

DATADIFF 函数返回两个日期之间的天数。你不应该在其中传递day 参数

SELECT DATEDIFF (Begindatum, Einddatum) AS Tijd FROM factuurregel

【讨论】:

  • 这仍然会给我这个错误: object(PDOStatement)#5 (1) { ["queryString"]=> string(66) "SELECT DATEDIFF (Begindatum, Einddatum) AS Tijd from factuurregel; " }
  • object(PDOStatement)#5 (1) { ["queryString"]=> string(66) "SELECT DATEDIFF (Begindatum, Einddatum) AS Tijd from factuurregel;" }
  • 你能帮帮我吗? @maximfedorov
  • 这看起来不像是错误。它看起来像一个 var_dump
  • 但这不是我需要的输出吗? @apokryfos
【解决方案2】:

错误表示参数计数无效。根据文档 - https://www.w3resource.com/mysql/date-and-time-functions/mysql-datediff-function.php:

SELECT DATEDIFF (Begindatum, Einddatum) AS Tijd from factuurregel

【讨论】:

  • 这仍然会给我这个错误: object(PDOStatement)#5 (1) { ["queryString"]=> string(66) "SELECT DATEDIFF (Begindatum, Einddatum) AS Tijd from factuurregel; " }
【解决方案3】:

如果您使用的是 MySQL,那么大概您想要:

SELECT DATEDIFF(Einddatum, Begindatum) AS Tijd
FROM factuurregel;

请注意,参数是相反的。 MySQL 和 SQL Server 都有名为 datediff() 的函数。它们不仅在参数的数量上有所不同,而且在日期的顺序上也有所不同。

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2013-08-20
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2018-10-23
    • 2010-09-22
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多