查询生成器:带有复合列的 IN 子句

query builder: IN clause with composite columns(查询生成器:带有复合列的 IN 子句)
本文介绍了查询生成器:带有复合列的 IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要在多列上创建一个 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 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)