問題描述
我需要在多列上創建一個 IN 條件,就像這樣
I need to create a IN conditions on multiple columns, like this
...
WHERE
(order_date, order_number) IN (
('2016-03-11', 3455453),
('2016-03-18', 83545454),
('2016-06-17', 5354544)
)
從這樣的數組開始:
$orders = [
['2016-03-11', 3455453],
['2016-03-18', 83545454],
['2016-06-17', 5354544]
];
使用 cake3 查詢構建器.我試過
using cake3 query builder. I tried with
->where(['(order_date, order_number) IN' => $orders]);
但我收到一個錯誤:
無法將值轉換為字符串
我知道手動創建操作數組的查詢并不難,但我想知道是否有一種方法可以做到這一點.
I know it's not hard to manually create the query manipulating the array, but I'd like to know if there is a cake way to do it.
推薦答案
AFAICT 這是不可能的(還)使用數組語法或正則比較表達式,負責轉換的代碼僅處理單個字段和平面數組,請參閱
AFAICT this is not possible (yet) using the array syntax or regular comparison expressions, the code responsible for transforming only handles single fields and flat arrays, see
來源>CakeDatabaseExpressionComparison::_stringExpression()
然而,這很可能使用元組比較表達式,它支持開箱即用的處理元組集.在內部,它被關聯用于處理復合鍵.
However, this is very well possible using a tuple comparison expression, which supports handling sets of tuples out of the box. Internally it is used by associations for handling composite keys.
$fields = ['order_date', 'order_number'];
$types = ['date', 'integer'];
$values = [
['2016-03-11', 3455453],
['2016-03-18', 83545454],
['2016-06-17', 5354544]
];
$query->where(
new CakeDatabaseExpressionTupleComparison($fields, $values, $types, 'IN')
);
來源> CakeDatabaseExpressionTupleComparison
這篇關于查詢生成器:帶有復合列的 IN 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!