doushuo8677
2016-01-08 02:27
浏览 657
已采纳

在SQL IN中使用数组

Two part question...(note that I'm using a PostGres)

My SQL query is formatted like this:

$.ajax({
        url: "https://something?q=SELECT *database_final_form_merge where territory in ("+terrs+")",
        type: 'GET',
        dataType: 'JSON',
     success: function(data) {
      }
       });

The variable terrs is an array like this:

["D1VE3011", "D1VE3011", "D1VD2209", "D1VD2209", "D1VD2103", "D1VD2103"]

This formats the SQL query like this though:

SELECT* from database_final_form_merge where territory IN (D1VE3011,D1VE3011,D1VD2209,D1VD2209,D1VD2103,D1VD2103)

But it needs to be in this format (I think):

SELECT* from database_final_form_merge where territory IN ('D1VE3011','D1VE3011','D1VD2209','D1VD2209','D1VD2103','D1VD2103')

This works when I try it directly without an AJAX GET. Is there a different way I should be passing this array?

That's question 1.

Question 2...is there a way to pass that array so that only unique values are passed? You'll note that in my array there are duplicates, but wondering if there's a way to only pass along unique values.

Thanks.

图片转代码服务由CSDN问答提供 功能建议

两部分问题......(请注意我正在使用PostGres)

我的SQL查询格式如下:

  $ .ajax({
 url:“https:// something?q = SELECT * database_final_form_merge where where in in(  “+ terrs +”)“,
类型:'GET',
 dataType:'JSON',
成功:函数(数据){
} 
}); 
    
 
 

变量 terrs 是这样的数组:

[“D1VE3011”,“D1VE3011”,“D1VD2209”, “D1VD2209”,“D1VD2103”,“D1VD2103”]

这样可以格式化这样的SQL查询:

   SELECT *来自database_final_form_merge,其中区域为IN(D1VE3011,D1VE3011,D1VD2209,D1VD2209,D1VD2103,D1VD2103)
   
 
 

但它需要采用这种格式(我认为):

  SELECT * from database_final_form_merge,其中区域为IN('D1VE3011','D1VE3011','D1VD2209','D1VD2209','D1VD2103','D1VD2103')
   
 
 

当我尝试使用它时,这是有效的 没有AJAX GET。 我应该通过这个数组采用不同的方式吗?

那是问题1。

问题2 ...有没有办法通过 数组,以便只传递唯一值? 你会注意到在我的数组中有重复项,但是想知道是否有办法只传递唯一值。

谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwen5019 2016-01-08 02:41
    已采纳

    Let's put passing query as a parameter aside and get into the problem.

    For the question 2 you can use jQuery.unique

    And for the former question:

    "('" + terrs.join("','") + "')" generates ('D1VE3011','D1VE3011','D1VD2209','D1VD2209','D1VD2103','D1VD2103') part.

    Mind the white spaces though. You might end up with string like this

    '(' D1VD2209',' D1VD2103','D1VD2103 ')

    *EDITED accordingly

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题