日期:2014-05-16  浏览次数:20955 次

请教一个另类sql语句的写法
我做了两个表
table1
ID                       content                 CID            
1                       aaa   bbb   ccc           null
2                       333   4444   55           null
3                       222     200                 null
4                       aaa   bbb   ccc           null
5                       333   4444   55           null
6                       222     200                 null

table2
CID                                       rule                                   title  
5002               where   content   like   "%bbb% "           bbbbbbb
3002               where   content   like   "%222% "           50505050


我想做的是写一条sql完成:
使用table2里的rule里的规则   去查找table1里的content  
如果符合条件把table2里的CID赋值给table1里的CID中,实现两个表关联。

比较绕,是不是架构很水,大家帮忙写下这个sql,谢谢

------解决方案--------------------
用 sp 可以实现这个需求。

这是一个简单的例子,你做为参考去实现你的需求吧:
SQL code

set @cmd = 'select * from a;';
PREPARE  sql_cmd from @cmd;
execute sql_cmd;

------解决方案--------------------
你的例子中,上述例子中的 @cmd 就用 concat 来生成你的语句。
------解决方案--------------------
存储过程可以实现。
------解决方案--------------------
set @cmd = 'select name into @xxxx from user where ID = 1';