原始查询(分成行以便我们阅读[提示])
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 最佳实践被重新定义,我们停止在表名之间使用逗号。 停下来...拜托! 无论如何你都不能在 Knex.js 中做到这一点....所以最好习惯它。先修复join语法:
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 好像也不太容易做别名,所以用表名替换:
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
});
- http://knexjs.org/#Builder-select
- http://knexjs.org/#Builder-from
- http://knexjs.org/#Builder-innerJoin
- http://knexjs.org/#Builder-where
- http://knexjs.org/#Interfaces-then