Knex.js : 如何从多个表中 select 列?
Knex.js : How to select columns from multiple tables?
示例查询,
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum FROM driverProfile a, carProfile b WHERE a.dManagerID = 7 AND b.carID=a.dCarID
查询在 MySQL 上运行良好。 driverProfile 和 carProfile 是两个独立的表。
如果您需要更多说明,请发表评论。我被困在这里。
感谢您的帮助。谢谢你。
原始查询(分成行以便我们阅读[提示])
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM driverProfile a, carProfile b
WHERE a.dManagerID = 7 AND b.carID=a.dCarID
第 1 步,连接语法(修复它!)
25 多年前,SQL 连接的最佳实践被重新定义,我们不再在 table 名称之间使用逗号。 快停下来吧...拜托! 而且你不能在 Knex.js 中做到这一点 ...所以最好习惯它。首先修复连接语法:
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM driverProfile a
INNER JOIN carProfile b ON b.carID=a.dCarID
WHERE a.dManagerID = 7
第 2 步,别名(不是)
Knex 似乎也不容易做别名,所以用 table 名字替换:
SELECT driverProfile.driverID, driverProfile.dCarID, driverProfile.dDeviceID, carProfile.carRegiNum
FROM driverProfile
INNER JOIN carProfile ON carProfile.carID=driverProfile.dCarID
WHERE driverProfile.dManagerID = 7
第3步,"Knexisfy"查询
knex.select(['driverProfile.driverID', 'driverProfile.dCarID', 'driverProfile.dDeviceID', 'carProfile.carRegiNum' ])
.from('driverProfile')
.innerJoin('carProfile','carProfile.carID','driverProfile.dCarID')
.where('driverProfile.dManagerID',7)
.then(function(output){
//Deal with the output data here
});
SELECT
a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM
driverProfile a,
carProfile b
WHERE
a.dManagerID = 7 AND b.carID=a.dCarID
使用 knex 0.14.0:
knex({ a: 'driverProfile', b: 'carProfile' })
.select('a.driverID', 'a.dCarID', 'a.dDeviceID', 'b.carRegiNum')
.where('a.dManagerID', 7)
.where('b.carID', knex.raw('??', ['a.dCarID']))
生成(https://runkit.com/embed/b5wbl1e04u0v):
select
`a`.`driverID`, `a`.`dCarID`, `a`.`dDeviceID`, `b`.`carRegiNum`
from
`driverProfile` as `a`, `carProfile` as `b`
where
`a`.`dManagerID` = ? and `b`.`carID` = `a`.`dCarID`
示例查询,
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum FROM driverProfile a, carProfile b WHERE a.dManagerID = 7 AND b.carID=a.dCarID
查询在 MySQL 上运行良好。 driverProfile 和 carProfile 是两个独立的表。 如果您需要更多说明,请发表评论。我被困在这里。
感谢您的帮助。谢谢你。
原始查询(分成行以便我们阅读[提示])
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM driverProfile a, carProfile b
WHERE a.dManagerID = 7 AND b.carID=a.dCarID
第 1 步,连接语法(修复它!)
25 多年前,SQL 连接的最佳实践被重新定义,我们不再在 table 名称之间使用逗号。 快停下来吧...拜托! 而且你不能在 Knex.js 中做到这一点 ...所以最好习惯它。首先修复连接语法:
SELECT a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM driverProfile a
INNER JOIN carProfile b ON b.carID=a.dCarID
WHERE a.dManagerID = 7
第 2 步,别名(不是)
Knex 似乎也不容易做别名,所以用 table 名字替换:
SELECT driverProfile.driverID, driverProfile.dCarID, driverProfile.dDeviceID, carProfile.carRegiNum
FROM driverProfile
INNER JOIN carProfile ON carProfile.carID=driverProfile.dCarID
WHERE driverProfile.dManagerID = 7
第3步,"Knexisfy"查询
knex.select(['driverProfile.driverID', 'driverProfile.dCarID', 'driverProfile.dDeviceID', 'carProfile.carRegiNum' ])
.from('driverProfile')
.innerJoin('carProfile','carProfile.carID','driverProfile.dCarID')
.where('driverProfile.dManagerID',7)
.then(function(output){
//Deal with the output data here
});
SELECT
a.driverID, a.dCarID, a.dDeviceID, b.carRegiNum
FROM
driverProfile a,
carProfile b
WHERE
a.dManagerID = 7 AND b.carID=a.dCarID
使用 knex 0.14.0:
knex({ a: 'driverProfile', b: 'carProfile' })
.select('a.driverID', 'a.dCarID', 'a.dDeviceID', 'b.carRegiNum')
.where('a.dManagerID', 7)
.where('b.carID', knex.raw('??', ['a.dCarID']))
生成(https://runkit.com/embed/b5wbl1e04u0v):
select
`a`.`driverID`, `a`.`dCarID`, `a`.`dDeviceID`, `b`.`carRegiNum`
from
`driverProfile` as `a`, `carProfile` as `b`
where
`a`.`dManagerID` = ? and `b`.`carID` = `a`.`dCarID`