在sql中创建一个变量数组

时间:2018-05-01 15:05:24

标签: sql

现在我的代码看起来像:

SELECT * 
  FROM Table 
 WHERE Id in ('123','456','789')

我想让列表成为变量,以便代码看起来像这样:

Id_list = ('123','456','789')

SELECT * 
  FROM Table 
 WHERE Id in Id_list

我不熟悉SQL,如果这很明显或我的描述使用了错误的术语,我很抱歉。谢谢!

4 个答案:

答案 0 :(得分:1)

在mysql中,mysql服务器:

SELECT *
  FROM (SELECT '1' 
        UNION SELECT '2' 
        UNION SELECT '3' 
        UNION SELECT '4') as Array

答案 1 :(得分:1)

一种可能的解决方案是将数组放入VARCHAR:

Id_list VARCHAR2(500)= '123,456,789';

SELECT * 
  FROM Table 
 WHERE INSTR(Id_list,Id) > 0

问候!

答案 2 :(得分:1)

这是sql server方法

  #include <iostream>
  #include <vector>
  struct MMT
  {
     int a;
     char b;
     int * data;
  }
  int func(void *structPtr){
     //use the structure member
  }
  int main ()
  {
      std::vector<MMT*> myvector;


      for (int i=1; i<=5; i++){
           MMT *mmt;
           mmt->a = i;
           mmt->b = 'a';
           myvector.push_back(MMT);
      }
      std::cout << "myvector contains:";
      for (std::vector<int*>::iterator it = myvector.begin() ; it !=myvector.end(); ++it)
      {
        func((void*)it);//?????????//how to pass structure
      }
      std::cout << '\n';
      return 0;
  }

在sql

中使用像temp一样的临时表

答案 3 :(得分:0)

SQL Server版本:

--Test Data
CREATE TABLE TestTable
    ([ID] nvarchar(20))
;

INSERT INTO TestTable
    ([ID])
VALUES
    ('123'),
    ('456'),
    ('789')
;

--Use With As
with arr_st as (
  select '123' ID union all
  select '456' ID union all
  select '789' ID
)
SELECT * FROM TestTable 
WHERE Id in (select * from arr_st)

结果:

SQL Fiddle LINK http://sqlfiddle.com/#!18/fbba7/2/0

希望它可以帮助你:)