mysql - 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
解决方案
metadata
. deleted_at
= null 应替换为metadata
. deleted_at
为空,这里有 Bohemian 的出色解释:https ://stackoverflow.com/a/9581790/6597774
推荐阅读
- flutter - 如何在 Flutter 中自定义 Slider 小部件?
- multithreading - 参数化测试以在 MSTEST 中运行并行或单线程
- c++ - 有没有更好的方法来检测我的向量中某个项目的多次出现?
- php - 将发送的发票作为最终发票
- go - Go 结构比较 - reflect.DeepEqual 在地图上失败?
- android - 如何将包含 Firebase 的 android 应用程序从一台计算机转移到另一台计算机
- angular - 延迟加载不会生成任何块Angular 8
- docker - Gitlab CI - 构建图像并传递到下一阶段?
- javascript - 如何使用 kendo ui 在从反应组件下载的 pdf 中添加页脚
- tsx - 如何添加自定义 HTML 标签 TSX