首页 > 解决方案 > Knex上的MySQL左外连接未返回正确的数据

问题描述

我正在对 Knex 中遇到的一些错误进行故障排除,并尝试对 2 个表(即通知和元数据)进行左连接,这两个表具有相同的 2 列,即我想要的“device_id”和“channel”匹配。但是,即使存在具有匹配 device_id 和通道的元数据记录 (metadata_id=1),以下查询字符串也不起作用并返回以下内容。

我检查了两个表中的 device_id 和 channel 的数据类型是否相同。卡住了一段时间,不知道这里出了什么问题,如果有人可以提供帮助,那就太好了!对于嵌套查询,在转换为 Knex 时也存在一些问题,但这可能是一个小问题。

{
    notification_id: 1,
    message: 'hello world',
    mode: 'email',
    metadata_id: null,
    unit_conversion: null
}

SELECT `notifications`.`notification_id`, `notifications`.`message`, `notifications`.`mode`, 
  `metadata`.`metadata_id`, `metadata`.`unit_conversion` from `notifications` 
  LEFT OUTER JOIN `metadata` ON (`metadata`.`device_id` = `notifications`.`device_id` AND 
  `metadata`.`channel` = `notifications`.`channel` AND `metadata`.`deleted_at` = null )
  WHERE `notifications`.`notification_id` = 1

标签: mysql

解决方案


metadata. deleted_at= null 应替换为metadata. deleted_at为空,这里有 Bohemian 的出色解释:https ://stackoverflow.com/a/9581790/6597774


推荐阅读