- 内联和左联
内联和左联
如果你想使用INNER JOIN
而不是LEFT JOIN
,只需使用innerJoinAndSelect
:
const user = await createQueryBuilder("user")
.innerJoinAndSelect("user.photos", "photo", "photo.isRemoved = :isRemoved", { isRemoved: false })
.where("user.name = :name", { name: "Timber" })
.getOne();
This will generate:
SELECT user.*, photo.* FROM users user
INNER JOIN photos photo ON photo.user = user.id AND photo.isRemoved = FALSE
WHERE user.name = 'Timber'
LEFT JOIN
和INNER JOIN
之间的区别在于,如果没有任何 photos,INNER JOIN
将不会返回 user。即使没有 photos,LEFT JOIN
也会返回 user。要了解有关不同连接类型的更多信息,请参阅 SQL 文档.