asp中怎样使用一个字符串中的字符查询另一个表中的记录

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:17:06
两个表格product,maker

表maker的中的maker_product字段存储的是表product中编号(id)的字符串多重形式,如maker表中记录的一个字段存储“ '1'+'2'+'3'+''4 ”,现在我想用上面这个字符串select出product表中对应id的记录,如果怎样实现最好呢?向大家求助方案,谢谢了先
'1'+'2'+'3'+'4' 就是怕到了多记录以后用 like %product_id% 回出错,比如id=1 二字符串有可能是'1'+'12'+'13'这样会不会就把这3条都给给找出来呢,但是想找的确是'1'这一条记录。

select count(*) from maker
where maker_id=001 and maker_product like '%''' & product_id & '''%'
这一句用到了%''' & product_id & '''%,这部分是在asp语句中加入的变量是吧,问题是给出的变量只有从maker表中取出的'1'+'2'+'3'+'4'字符串,product_id是通过查询得出来的出来的。

maker_produc=rs("maker_product")
select * from product where '"& maker_product &"' like 'product_id'
这样写可以么?问题是product_id是表中的字段,这样使用可以么?还请你详细说明谢谢或者还有什么好的思路。
目的就是后台maker表中在添加记录时在maker_product字段中存入了多个product表中的pro

楼主的问题补充得对,呵呵,不好意思,今天早上在忙看日食,
只是想给你个提示,对于你的问题,我这个查询是可以胜任的!

例如:
product_id=1
maker_product like %1% 是会把 1 , 12 ,13 查出来,

但如果我改成这样呢:
maker_product like '%''' & product_id & '''%'

(SQL语句中,两个单引号,当一个单引号)
意思: '1' 两个单引号和ID构成为一个完整的字符串
这样就不会再查出 12 , 13 ,14 , 21 , 31 ... 了

当然,product_id = 13 相当于" '1'+'13'+'14'+'15' " like %'13'%
就只识别在 '1'+'13'+'14'+'15'+'131'
里的 '13' 而不会识别 '131' !

select product_id
from
( select product_id ,
( select count(*) from maker
where maker_id=001 and maker_product like '%''' & product_id & '''%'
) as isOk
from product
)
where isOk > 0

同意一楼。

这个程序结果以后麻烦多多。如果来得及变更下数据库设计思路。

这样