mysql - LEFT/RIGHT 在 node-orm2 上连接

标签 mysql node.js join node-orm2

我在我的 Node 开发中使用 dresende/node-orm2 ORM。但我需要进行一些左/右连接,但我在文档中找不到如何操作。我在尝试进行多个连接时也遇到了问题。

这段代码:

crModel.hasOne ('client', cliModel, {

    field:  'client_id'
});

crModel.hasOne ('office', boModel, {

    field:  'bo_id'
});

crModel.findByClient ({}).findByOffice ({}).find ({

    client_id:  1,
    bo_id:      1
}, function () {

    console.log (arguments);
});

生成此查询:

SELECT `t1`.`cr_id`, `t1`.`cr_datetime`, `t1`.`credit_id`, `t1`.`gs_id`, `t1`.`cellphone_id`, `t1`.`bo_id`, `t1`.`client_id` FROM `CreditRequests` `t1` JOIN `BranchOffices` `t2` ON `t2`.`bo_id` = `t1`.`bo_id` WHERE `t1`.`client_id` = 1 AND `t1`.`bo_id` = 1

谢谢,对我糟糕的英语感到抱歉。

最佳答案

ORM 的技巧是设想关系,而不是让它发挥作用的命令。对于 ORM2,有一个 hasMany 关联 - 我将从那里开始。来自文档:

patient.getDoctors(function..)           // List of doctors
patient.addDoctors(docs, function...)    // Adds entries to join table
patient.setDoctors(docs, function...)    // Removes existing entries in join table, adds new ones
patient.hasDoctors(docs, function...)    // Checks if patient is associated to specified doctors
patient.removeDoctors(docs, function...) // Removes specified doctors from join table

doctor.getPatients(function..)
etc...

// You can also do:
patient.doctors = [doc1, doc2];
patient.save(...)

https://github.com/dresende/node-orm2

关于mysql - LEFT/RIGHT 在 node-orm2 上连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059191/

相关文章:

php - 从 2 个组合表中获取最后一个条目

mysql - 如何在 UPDATE 语句中使用 JOIN?

mysql - 合并单个表上多个 select 语句的结果

javascript - 将文件从 Node 发送到客户端以供下载

javascript - 在javascript中从浏览器(客户端站点)中的url强制下载youtube视频

MYSQL:如何在同一查询中联接两个表,两次引用同一个表

mysql - 如何在MYSQL中计算两个字符串之间的相似度

mysql - Ruby Mysql 无法连接到本地主机 (10061)

mysql - 将带有千位分隔符逗号的小数插入MySQL数据库

node.js - docker-compose “up”与 “run”会产生不同的安装量