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

在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 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选