我有一个字符串列表,我想在SQL查询中多次传递。
我想声明它们一次,然后将列表传递给查询。这有可能吗?
值:
'1-PGB4Z', '1-PGAT6', '1-PGB55', '1-PGB52',
'1-2SS1E6', '1-2SS1E3', '1-2RZQMZ', '1-2RZQNF', '1-2RZQMT', '1-2SACTZ',
'1-2S0GNR', '1-2S0GLP', '1-2S0GCN', '1-1VOMUX', '1-1VOMV0', '1-1VOMV3',
'1-1VOX7Z', '1-1VOX82', '1-1VOMV7', '1-1VOMVA', '1-1VOMVD', '1-1VOMVG',
'1-1VPMBV', '1-1VPMBY', '1-1VPMC4', '1-1VO9XD', '1-1VO9XG', '1-1VO9XJ',
'1-1VPMBY', '1-1VO9XJ', '1-1VO9XM', '1-1VO9Z6', '1-16PGRE',
'1-16PGRK', '1-16PGRH', '1-8MNWB', '1-9JAVH', '1-8ISTX', '1-N5KYK',
'1-KZMMH', '1-D7KG9', '1-9HCNP', '1-9H81K', '1-1C9F1P', '1-QBQQW',
'1-2S0F1P', '1-2S0F45', '1-2S17LK', '1-2S17LT', '1-2SL5D9', '1-2SDUQD'
查询的一部分:
select * from table1 t3
where
and t1.cse_id in
(select d.cse_id
from table1 d
where d.cse_employee_number in
(select substr(t2.emp_s3_id, 3,8) from table2 t2, table 3 T1
where t2.fk_resource_group_id = t1.resource_group_id
and t1.resource_group_id in (
'1-PGB4Z', '1-PGAT6', '1-PGB55', '1-PGB52',
'1-2SS1E6', '1-2SS1E3', '1-2RZQMZ', '1-2RZQNF', '1-2RZQMT', '1-2SACTZ',
'1-2S0GNR', '1-2S0GLP', '1-2S0GCN', '1-1VOMUX', '1-1VOMV0', '1-1VOMV3',
'1-1VOX7Z', '1-1VOX82', '1-1VOMV7', '1-1VOMVA', '1-1VOMVD', '1-1VOMVG',
'1-1VPMBV', '1-1VPMBY', '1-1VPMC4', '1-1VO9XD', '1-1VO9XG', '1-1VO9XJ',
'1-1VPMBY', '1-1VO9XJ', '1-1VO9XM', '1-1VO9Z6', '1-16PGRE',
'1-16PGRK', '1-16PGRH', '1-8MNWB', '1-9JAVH', '1-8ISTX', '1-N5KYK',
'1-KZMMH', '1-D7KG9', '1-9HCNP', '1-9H81K', '1-1C9F1P', '1-QBQQW',
'1-2S0F1P', '1-2S0F45', '1-2S17LK', '1-2S17LT', '1-2SL5D9', '1-2SDUQD',
'1-2SDUTD', '1-2FRRMN', '1-2GHUNX', '1-2FXDD6', '1-2FWFG1', '1-2FR8CR',
'1-2FWFLM', '1-2FH7YL', '1-2FRRMX', '1-2GDIE3', '1-2FWFQI', '1-2FWFNL',
'1-2U9ULR', '1-2U4ZZP', '1-2SC2WF', '1-2SXPKJ', '1-2TDSJ5')
答案 0 :(得分:1)
最好将这些值存储在“查找”表中,并将其用作查询中的条件。如果值随后发生变化,则无需更新任何使用它们的存储过程。
例如
select * from table1 t3
where
and t1.cse_id in
(select d.cse_id
from table1 d
where d.cse_employee_number in
(select substr(t2.emp_s3_id, 3,8) from table2 t2, table 3 T1
where t2.fk_resource_group_id = t1.resource_group_id
and t1.resource_group_id in (SELECT value from lookup table)
据我所知,没有办法直接创建一个数组变量,但你可以创建一个VARCHAR变量,可以与其他函数一起使用来模拟搜索数组。但是,这不会直接在IN
语句
答案 1 :(得分:0)
如果这是“永久”值,请用它们填充表格并加入
答案 2 :(得分:0)
无法定义逗号分隔值并将它们用作IN()语句(不起作用)。 临时表是唯一的解决方案。
答案 3 :(得分:0)
方式2:
CREATE PROCEDURE exe_str(command TINYTEXT)/*exec the string*/
BEGIN
PREPARE stmt1 FROM command;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END
现在,您可以创建SELECT语句的字符串,然后执行它:
SET @str1="SELECT .........";
SET @str2="'1-PGB4Z', '1-PGAT6'........";
SET @str3=");";
CALL exe_str(CONCAT(@str1,@str2,str3));
我建议第一种方式。