MySQLi在where子句中使用date(now())连接

时间:2017-10-20 19:57:05

标签: php mysql mysqli

我在phpmyadmin中运行此查询,它工作正常。但是在joshcam's MySQLi Database课程中运行它会得到错误的数据 查询:

SELECT 
    s.az
    , s.ta
    , s.zamanSarfShode
    , p.name
FROM 
    saateruzane s 
JOIN 
    projhe p 
JOIN 
    kareruzane k
WHERE 
    s.ProjheId = p.id
AND 
    k.id = s.ruzId 
AND 
    k.ruzGregorian = date(now())   

PHP代码:

$con->join('projhe p', 's.ProjheId = p.id');
$con->join('kareruzane k', 'k.id = s.ruzId');
$con->joinWhere('kareruzane k','k.ruzGregorian', 'date(now())');
$tines = $con->get('saateruzane s',null,'s.az ,s.ta ,s.zamanSarfShode ,p.name');

2 个答案:

答案 0 :(得分:2)

joinWhere的定义是

public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')

所以你不需要传递每个部分。相反,试试这个:

$con->joinWhere('kareruzane k','k.ruzGregorian = date(now())');

虽然优于date(now()) CURDATE()

$con->joinWhere('kareruzane k','k.ruzGregorian = curdate()');

但是,我不确定您是否应该在此处使用联接,因为它是原始查询的WHERE子句的一部分,而不是JOIN子句。所以反而做

$con->where('k.ruzGregorian = curdate()');

答案 1 :(得分:0)

快速浏览一下这个lib文档,我发现了这个:

'createdAt' => $db->now(),
// createdAt = NOW()

https://github.com/joshcam/PHP-MySQLi-Database-Class#insert-query

在您的情况下,请尝试使用

$con->joinWhere('kareruzane k','k.ruzGregorian', $con->now());