CakePHP 3.6.14
此代码复制了错误的数字:
$where = [
'Postings.source' => $source,
'Postings.approved' => 1,
'Postings.deleted' => 0,
'Postings.disabled' => 0
];
if ($source !== null) {
$where['Postings.created >='] = '(NOW() - INTERVAL 3 DAY)';
}
$count = $this->Postings
->find()
->where($where)
->count();
debug($count); exit;
// 77568 total of all records
########## DEBUG ##########
[
'Postings.source' => 'xzy',
'Postings.approved' => (int) 1,
'Postings.deleted' => (int) 0,
'Postings.disabled' => (int) 0,
'Postings.created >=' => '(NOW() - INTERVAL 3 DAY)'
]
//SQL produced by this query:
SELECT (COUNT(*)) AS `count`
FROM postings Postings
WHERE (
Postings.source = 'xzy'
AND Postings.approved = 1
AND Postings.deleted = 0
AND Postings.disabled = 0
AND Postings.created >= '(NOW() - INTERVAL 3 DAY)' // <<<< with quotes
)
但原始sql查询:
SELECT COUNT(*) as `count
FROM `postings`
WHERE `source` = 'xzy'
AND `approved` = 1
AND `deleted` = 0
AND `disabled` = 0
AND `created` >= (NOW() - INTERVAL 3 DAY) // <<< without quotes
// return correct num 2119
如何解决?
答案 0 :(得分:2)
key => value
条件右侧的值始终受绑定/强制转换/转义的约束,除非它是一个表达式对象。查看生成的查询,您的SQL代码段将最终以字符串文字形式出现,即:
created >= '(NOW() - INTERVAL 3 DAY)'
长话短说,请使用一个表达式,可以是原始表达式:
$where['Postings.created >='] = $this->Postings->query()->newExpr('NOW() - INTERVAL 3 DAY');
或使用功能生成器:
$builder = $this->Postings->query()->func();
$where['Postings.created >='] = $builder->dateAdd($builder->now(), -3, 'DAY');
另请参见
答案 1 :(得分:0)
您应该使用查询构建器并将其添加到def stuff = udf[Vector, Vector, Vector]((v1,v2) => {
Vectors.dense(v1.toDense.values.zip(v2.toDense.values)
.map{case (a,b) => a+b})
.toSparse
})
test.groupBy("id").agg(stuff($"stuff")).show
java.lang.ClassCastException: $anonfun$stuff$1 cannot be cast to scala.Function1
at org.apache.spark.sql.catalyst.expressions.ScalaUDF.<init>(ScalaUDF.scala:98)
at org.apache.spark.sql.expressions.UserDefinedFunction.apply(UserDefinedFunction.scala:71)
... 87 elided
方法select
函数中。
此处描述了所有内容:https://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions