添加HAVING表达式

添加HAVING表达式很简单:

  1. createQueryBuilder("user").having("user.name = :name", { name: "Timber" });

将会生成以下 SQL 语句:

  1. SELECT ... FROM users user HAVING user.name = 'Timber'

你可以添加 AND 到已经存在的 HAVING 表达式中:

  1. createQueryBuilder("user")
  2. .having("user.firstName = :firstName", { firstName: "Timber" })
  3. .andHaving("user.lastName = :lastName", { lastName: "Saw" });

将会生成以下 SQL 语句:

  1. SELECT ... FROM users user HAVING user.firstName = 'Timber' AND user.lastName = 'Saw'

你可以添加 OR 到已经存在的 HAVING 表达式中:

  1. createQueryBuilder("user")
  2. .having("user.firstName = :firstName", { firstName: "Timber" })
  3. .orHaving("user.lastName = :lastName", { lastName: "Saw" });

将会生成以下 SQL 语句:

  1. SELECT ... FROM users user HAVING user.firstName = 'Timber' OR user.lastName = 'Saw'

你可以根据需要组合尽可能多的ANDOR表达式。如果使用多个.having,后面的将覆盖所有之前的HAVING表达式。