php - 如何使用 NULL-safe 等于 mysql operator with idiorm & paris

标签 php mysql

我想用 idiorm & paris 数据库工具包 ( http://j4mie.github.io/idiormandparis/ ) 提出这个请求:

SELECT mqtt_table.* FROM `shelf_mqtt_confirmation_message` `mqtt_table` WHERE NOT( `confirm_result` <=> 'positive')

我尝试过:

$orm->where_not_equal("confirm_result","positive");

但这等同于:

SELECT mqtt_table.* FROM `shelf_mqtt_confirmation_message` `mqtt_table` WHERE NOT( `confirm_result` = 'positive')

这排除了 confirm_result 为 NULL 的所有情况:但我也想保留这些行。问题在于,在 MySQL 中,如果要比较的元素之一为 NULL 而不是返回 0,则 = 比较运算符返回 NULL。(当 <=> 执行类似于 = 运算符的相等比较时,但如果两个操作数均为 NULL,则返回 1 而不是 NULL , 如果一个操作数为 NULL,则为 0 而不是 NULL)

所以我的问题是,运算符 <=> 是否存在于 idiorm & paris 中?除了使用 raw_query 或 where_raw 等残酷方法之外,是否有一些解决方案可以满足我想做的请求?

最佳答案

这可以通过 where_raw()docs link 来完成方法。

可以重写以下 SQL 查询

SELECT *
  FROM shelf_mqtt_confirmation_message
 WHERE NOT(confirm_result <=> 'positive')

在以下代码中使用成语

$messages = ORM::for_table('shelf_mqtt_confirmation_message')
    ->where_raw('NOT(confirm_result <=> ?)', array('positive'))
    ->find_many();

Parisdocs link可能是这样的

$messages = ShelfMqttConfMessage::where_raw(
        'NOT(confirm_result <=> ?)', array('positive')
    )
    ->find_many();

或者如果您改用模型工厂

$messages = Model::factory('ShelfMqttConfMessage')
    ->where_raw('NOT(confirm_result <=> ?)', array('positive'))
    ->find_many();

关于php - 如何使用 NULL-safe 等于 mysql operator with idiorm & paris,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42420258/

相关文章:

php - 在 WooCommerce 电子邮件模板中获取产品名称和描述

mysql - 使用jmeter 5.1.1版本无法连接到sql server

java - 从非事务方法调用的多个事务方法的传播级别

php explode mysql 几何线串类型

php - 具有多字节字符编码支持的 HTML 到图像/pdf 转换器

php - 获取 MySQL 查询结果作为其原生数据类型?

php - 从数据库中出来的 float 错误

mysql - 无法从 liquibase gradle 插件中生成差异

php - 表格内的下拉菜单 - 然后更新

php - 清理/替换所有日语、中文、韩语、俄语等字符