我有以下简单的表结构和准备好的声明:
create table junk1(name varchar(10), state varchar(10));
create table junk2(state varchar(100));
insert into junk2(state)values("state like '%Tex%'"),("state like '%Neb%'"),("state like '%Was%'");
insert into junk1(name, state) values ('asa', 'Texas'), ('dff', 'Washing'), ('fgfgf', 'Oklahoma'), ('bbb', 'Nevada'), ('hhh', 'Texas'), ('jjj', 'Nebraska');
SET @va = '';
select group_concat(state separator ' OR ') into @va from junk2;
PREPARE stmt1 FROM 'select * from junk1 where ?';
execute stmt1 using @va;
deallocate prepare stmt1;
我没有按预期得到结果。我没有结果。我在这里做错了什么?
答案 0 :(得分:0)
您不能在预准备语句中使用语法元素作为参数,即您不能将整个where子句作为参数传递,只能传递值。
您可能希望将where子句连接起来:
select group_concat(state separator '" OR "') into @va from junk2;
set @clause = concat('select * from junk1 where "', @va, '"');
PREPARE stmt1 FROM @clause;