简体   繁体   English

SOQL查询中的LIKE或CONTAINS

[英]LIKE or CONTAINS in SOQL Query

I'm trying obtain some data from the following URL, using a JavaScript code: 我正在尝试使用JavaScript代码从以下URL获取一些数据:

http://data.cityofnewyork.us/resource/erm2-nwe9.json http://data.cityofnewyork.us/resource/erm2-nwe9.json

That's how I construct my query: 这就是我构建查询的方式:

//data URL variables
var start_date = '2013-08-01'; //YYYY-MM-DD
var end_date = '2013-08-08';   //YYYY-MM-DD
var c_type = 'Noise';          // Complaint Type

// Build the data URL
var URL = "http://data.cityofnewyork.us/resource/erm2-nwe9.json"; // API Access Endpoint
URL += "?";                                                   
URL += "$where=";                                             
URL += "(latitude IS NOT NULL)";                              
URL += " AND ";
URL += "(complaint_type='" + c_type + "')";                    
URL += " AND ";
URL += "(created_date>='" + start_date + "') AND (created_date<='" + end_date + "')"; 
URL += "&$group=complaint_type,descriptor,latitude,longitude";                        
URL += "&$select=descriptor,latitude,longitude,complaint_type";                       
URL = encodeURI(URL);

And how I'm testing it so far: 到目前为止我是如何测试它的:

$.getJSON(URL, function(data)
{
        console.log(data)
});

Right now it works fine, but I should consider any complaint type that contains a single world ("Noise"): 现在它工作正常,但我应该考虑任何包含单个世界的投诉类型(“噪音”):

URL += "(complaint_type LIKE '%" + c_type + "%')";

Encoded URL (seems OK): 编码的URL(似乎没问题):

http://data.cityofnewyork.us/resource/erm2-nwe9.json ?$where=(latitude%20IS%20NOT%20NULL)%20AND%20(complaint_type%20LIKE%20'%25Noise%25')%20AND%20(created_date%3E='2013-08-01')%20AND%20(created_date%3C='2013-08-08')&$group=complaint_type,descriptor,latitude,longitude&$select=descriptor,latitude,longitude,complaint_type http://data.cityofnewyork.us/resource/erm2-nwe9.json?$ where =(纬度%20IS%​​20NOT%20NULL)%20AND%20(complaint_type%20LIKE%20'%25Noise%25')%20AND% 20(CREATED_DATE%3E = '2013年8月1日')%20于是%20(CREATED_DATE%3C = '2013年8月8日')&$组= complaint_type,描述符,纬度,经度及$选择=描述符,纬度,经度,投诉类型

Error: 错误:

{
  "code" : "query.compiler.malformed",
  "error" : true,
  "message" : "Error, could not parse SoQL query \"select descriptor,latitude,longitude,complaint_type from #erm2-nwe9 where (latitude IS NOT NULL) AND (complaint_type LIKE '%Noise%') AND (created_date>='2013-08-01') AND (created_date<='2013-08-08') group by complaint_type,descriptor,latitude,longitude\"",
  "data" : {
    "query" : "select descriptor,latitude,longitude,complaint_type from #erm2-nwe9 where (latitude IS NOT NULL) AND (complaint_type LIKE '%Noise%') AND (created_date>='2013-08-01') AND (created_date<='2013-08-08') group by complaint_type,descriptor,latitude,longitude"
  }
}

The documentation seems that it is possible to use LIKE, but I can't get it to work. 文档似乎可以使用LIKE,但我不能让它工作。

I don't know how to do this. 我不知道该怎么做。

I have just figured out how it works, it seems that it does not accept just percent symbols '%' in the condition, it should be preceded by backslash(or is it just slash, anyway). 我刚刚弄清楚它是如何工作的,它似乎不接受条件中的百分号'%',它应该以反斜杠开头(或者它只是斜线,无论如何)。

So this is the valid URL for using 'like' statement: 所以这是使用'like'语句的有效URL:

http://data.cityofnewyork.us/resource/erm2-nwe9.json?$where=(latitude%20IS%20NOT%20NULL)%20AND%20(complaint_type%20like%20%27\\%Noise\\%%27)%20AND%20(created_date%3E=%272013-08-01%27)%20AND%20(created_date%3C=%272013-08-08%27)&$group=complaint_type,descriptor,latitude,longitude&$select=descriptor,latitude,longitude,complaint_type

Or appropriate row in your code: 或者代码中的相应行:

URL += "(complaint_type LIKE '\\%" + c_type + "\\%')";

Let me know if it works, and sorry for not replying on time, I really have no experience with Salesforce and SOQL. 让我知道它是否有效,并且抱歉没有按时回复,我真的没有使用Salesforce和SOQL的经验。 But thanks to you there is another new space for me to explore :) 但多亏了你,还有另一个新的空间供我探索:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM