Zoho crm Logo

子查询

子查询是嵌套或嵌入在父查询(也称为外部查询)中的子查询。子查询可以作为查询中JOINs的替代方案。当JOIN字段只在条件中需要时,即在WHERE子句中,使用子查询。使用子查询的主要原因是通过节省时间和内存来提高性能。 
子查询的结果将用于执行父查询。这种方法对于基于相关记录过滤数据特别有用。 
 

注意

您可以在单个查询中检索每个子查询最多100条记录。在父查询中,最多可以查询5个子查询。

子查询结构

{
"select_query" : "select Lead_Source,Email from Leads where Contacts in (select id from Contacts where Email='patricia@mail.com')"
}

注意

使用子查询,你只需要在查询中引用一次查找字段,而不是像在连接中多次引用。

示例:不使用子查询的查询(使用JOIN的查询)

{
 "select_query" : "select Full_Name from Contacts where ((Account_Name.Account_Name='Patricia' ) and (Account_Name.Account_Type='Press'))"
}

示例:带子查询的查询

在使用子查询时,Account_Name 查找字段只被引用一次。

{
"select_query": "Select Full_Name from Contacts where Account_Name in (select id from Accounts where (Account_Type='Press') and (Account_Name='Patricia'))"
}

子查询 vs 连接

下表显示了何时使用子查询和何时使用JOIN。

子查询JOIN
在WHERE子句中查询查找内部字段,例如Account_Name.Account_Name。

要在WHERE子句之外查询查找的内部字段,必须使用JOIN。

示例: 

{ 

"select_query": "Select Full_Name, Account_Name.Account_Type, Account_Name.Account from Contacts where Account_Name in (select id from Accounts where (Account_Type='Press') and (Account_Name='A'))" 

}

 

在这里,"Account_Name"代表从Contacts模块指向Accounts模块的查找字段。

 

在单个查询中,最多可以检索100条记录。在父查询中,最多可以查询五个子查询。

 

要检索超过100条记录,请使用JOIN。

参考以下情景以便更好地理解。

场景

想象一下,检索与账户名称(查找内部字段)以"A"开头的联系人的全名。
 

假设以下记录数来自账户和联系人模块:

  • 在账户模块中,有400条记录以"A"开头。
  • 在联系人模块中,
    • 有1600条记录与以"A"开头的400个账户相关联。
    • 在上述400个账户中,有350个联系人记录与前100个账户相关联,这些账户是按照ORDER BY子句和默认的ID字段(使用ID字段的默认升序排序)排序的,其中账户名称以"A"开头。 

让我们看看使用JOIN和子查询进行查询时的结果:

使用JOIN进行查询

{
 "select_query" : "select Full_Name from Contacts where Account_Name.Account_Name like 'A%' limit 2000"
}

上述查询将返回最多2000条以“A”开头的联系人的记录。由于有1600条匹配的记录,此查询将在一次请求中检索到所有这些记录。

使用子查询进行查询

{
"select_query" : "select Full_Name from Contacts where Account_Name in (select id from Accounts where Account_Name like 'A%') LIMIT 2000"
}

子查询首先从账户模块中检索以"A"开头的账户的ID。一旦检索到账户ID,查询就会在联系人模块中搜索与这些账户关联的所有联系人。


在这种情况下,单个子查询一次只能检索100个账户记录。由于有400个账户的名称以"A"开头,但子查询只获取前100个账户,这导致查询只检索到350个联系人记录(假设这350个联系人与前100个账户相关联)。


在这种情况下,如果用户知道将返回超过100条记录(就像这里的400个账户记录,其账户名称以"A"开头),建议使用JOIN而不是子查询。

支持的运算符

注意

在以下部分中,“操作数”一词指的是操作符处理的子查询或直接值。

子查询支持操作符

多值运算符: 支持多个值作为操作数。 

  • 在
  • 不在

单值运算符:支持单个值作为操作数。

  • = (等于)
  • != (不等于)
  • < (小于)
  • > (大于)
  • <= (小于或等于)
  • >= (大于或等于)
  • 之间
  • 不在之间

 注意:在使用BETWEEN和NOT BETWEEN操作符与子查询时,确保每个子查询返回一个单一值。如果没有,系统将抛出错误。

示例:

带有两个子查询操作数的样本查询

以下查询检索了在第一条记录的创建时间和第201条记录的创建时间之间创建的潜在客户Created_Time。

{
 "select_query" : "select Last_Name from Leads where Created_Time between (select Created_Time from Leads where id is not null Limit 1) and (select Created_Time from Leads where id is not null limit 200,1)"
}

带有单个子查询操作数的示例查询

以下查询检索了创建时间在第一条记录的创建时间和指定日期时间范围(2024-09-25T23:59:59+05:30)之间的潜在客户。

{
 "select_query" : "select Last_Name from Leads where Created_Time between (select Created_Time from Leads where id is not null Limit 1) and '2024-09-25T23:59:59+05:30'"
}

子查询序列

子查询序列是根据执行顺序分配给每个子查询的唯一标识符。执行从0th索引开始,意味着首先执行的子查询获得序列"0",下一个为"1",依此类推。

{
"select_query" : "select id from Contacts where (Account_Name in (select id from Accounts where id is not null) and  Created_Time between (select Created_Time from Accounts where Account_Name like 'A%' Limit 1) and (select Created_Time from Accounts where Account_Name like 'B%' Limit 1)) and  Vendor_Name in (select id from Vendors where id in (select id from Vendors where Vendor_Name like 'V%'))"
}

执行过程

以下是外部查询:

select id from Contacts where (Account_Name in (select id from Accounts where id is not null) and  Created_Time between (select Created_Time from Accounts where Account_Name like 'A%' Limit 1) and (select Created_Time from Accounts where Account_Name like 'B%' Limit 1)) and  Vendor_Name in (select id from Vendors where id in (select id from Vendors where Vendor_Name like 'V%'))
        

这个外部查询依赖于几个必须按顺序执行的子查询: 

  • 第一个子查询,(从Accounts中选择id,其中id不为空),是独立的,并且首先执行,序列为"0"。
  • 第二个子查询,(select Created_Time from Accounts where Account_Name like 'A%' Limit 1),也是独立的,并且接下来执行,序列为"1"。
  • 接下来执行的是第三个子查询,(select Created_Time from Accounts where Account_Name like 'B%' Limit 1),其顺序为"2"。
  • 第四个子查询,(select id from Vendors where id in (select id from Vendors where Vendor_Name like 'V%')),依赖于第五个子查询(select id from Vendors where Vendor_Name like 'V%')。
    • 第五个子查询首先执行,其顺序为"3"。
    • 然后,第四个子查询执行,使用第五个子查询的结果和其顺序"4"。 

限制:

  • 子查询只能在WHERE子句中指定。
  • 一个单一查询可以包含多达五个子查询。
  • 每个子查询只接受一个SELECT列。
     

可能的错误

  • INVALID_QUERYHTTP 400
    • 子查询已在不支持的子句中指定
      解决方案:只在WHERE子句中指定子查询。
    • 一个或多个子查询包含多个SELECT列
      解决方案:确保每个子查询只包含一个SELECT列。
    • 子查询返回多个值,与操作符(单值操作符)不兼容
      解决方案:子查询基于子查询序列执行。要么使子查询返回单个值,要么使用多值操作符,如IN和 NOT IN。
  • LIMIT_EXCEEDEDHTTP 400
    • 您的外部查询已达到其允许的子查询限制
      解决方案:请在允许的限制内指定您的子查询。允许的限制是五。
    • 子查询检索的记录数超过了允许的限制
      解决方案:每个子查询只能检索最多100条记录。请相应地设置限制。