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

在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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合