添加HAVING
表达式
添加HAVING
表达式很简单:
createQueryBuilder("user").having("user.name = :name", { name: "Timber" });
将会生成以下 SQL 语句:
SELECT ... FROM users user HAVING user.name = 'Timber'
你可以添加 AND
到已经存在的 HAVING
表达式中:
createQueryBuilder("user")
.having("user.firstName = :firstName", { firstName: "Timber" })
.andHaving("user.lastName = :lastName", { lastName: "Saw" });
将会生成以下 SQL 语句:
SELECT ... FROM users user HAVING user.firstName = 'Timber' AND user.lastName = 'Saw'
你可以添加 OR
到已经存在的 HAVING
表达式中:
createQueryBuilder("user")
.having("user.firstName = :firstName", { firstName: "Timber" })
.orHaving("user.lastName = :lastName", { lastName: "Saw" });
将会生成以下 SQL 语句:
SELECT ... FROM users user HAVING user.firstName = 'Timber' OR user.lastName = 'Saw'
你可以根据需要组合尽可能多的AND
和OR
表达式。如果使用多个.having
,后面的将覆盖所有之前的HAVING
表达式。