如何为WHERE IN()SQL查询选择转换列表varchar

时间:2015-08-24 08:54:35

标签: sql select stored-procedures

如何选择转换列表varchar WHERE IN()SQL查询

 select code from table1 
 Code
 10223
 10240
 10047
 10174
 10187
 10192
 10212
 10213
 10195


  select * table2 from where code in 
  ('10047','10174','10187','10192','10212','10213','10195')

我需要这个

的存储过程

2 个答案:

答案 0 :(得分:2)

in运算符可以对查询进行操作,因此无需进行此类转换:

SELECT *
FROM   table2
WHERE  code IN (SELECT code FROM table1)

答案 1 :(得分:0)

您有两种选择:

使用加入:

假设Code中的table1列是唯一的:

SELECT t2.* FROM table2 t2 INNER JOIN table1 t1 ON CAST(t1.code AS VARCHAR)=CAST(t2.code AS VARCHAR)

内部查询

SELECT * FROM  table2 WHERE code IN 
(SELECT code FROM table1)