如:我要查询tb_mgoodsmoretype中编号为10,121的数据,并根据其编号gmt_Id_i 在tb_mgoods 表中统计这个编号对应的数据条数 补充:goods_MoreType_v的数据格式为[10][12],或者[121][1789]
select gmt_Id_i ,gmt_Name_v,
(select count(*) from tb_mgoods where goods_MoreType_v like '%gmt_Id_i%') as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121)
哦上面的条件是这样的 like '%[gmt_Id_i]%' 因为goods_MoreType_v的格式是这样的 [10][121]
请看清问题我是要用LIKE,不是直接等于!因为goods_MoreType_v数据格式是 [10][121] 之类的,而我的gmt_Id_i数据是编号 10 等
【select gmt_Id_i ,gmt_Name_v,
(select count(*) from tb_mgoods where goods_MoreType_v=tb_mgoodsmoretype.gmt_Id_i) as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121) 】
select t.gmt_Id_i ,t.gmt_Name_v,
( select count(*) from tb_mgoods where goods_MoreType_v like '%/[' + t.gmt_Id_i + '/]%' escape '/') as counts
from tb_mgoodsmoretype t where t.gmt_Id_i in(10,121)
注意: like '%某字符串%' 等效于 like '%'+'某字符串'+'%',由于gmt_Id_i是字段名称,故不能使用 like '%gmt_Id_i%' 必须使用 like '%' + gmt_Id_i + '%' ,另外由于你的goods_MoreType_v字段含有[],必须使用转义符,把[]转为字符串[],否则,sql会理解[]为通配符使用,还有 sql中转义符\ 解释为把该字符后的第一个字母转成普通的字符。
( select count(*) from tb_mgoods where goods_MoreType_v like '%/[' + t.gmt_Id_i + '/]%' escape '/') as counts
from tb_mgoodsmoretype t where t.gmt_Id_i in(10,121)
注意: like '%某字符串%' 等效于 like '%'+'某字符串'+'%',由于gmt_Id_i是字段名称,故不能使用 like '%gmt_Id_i%' 必须使用 like '%' + gmt_Id_i + '%' ,另外由于你的goods_MoreType_v字段含有[],必须使用转义符,把[]转为字符串[],否则,sql会理解[]为通配符使用,还有 sql中转义符\ 解释为把该字符后的第一个字母转成普通的字符。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-05-23
select gmt_Id_i ,gmt_Name_v,
(select count(*) from tb_mgoods where goods_MoreType_v=tb_mgoodsmoretype.gmt_Id_i) as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121)
(select count(*) from tb_mgoods where goods_MoreType_v=tb_mgoodsmoretype.gmt_Id_i) as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121)
第2个回答 2010-06-06
select gmt_Id_i ,gmt_Name_v,
(select count(*) from tb_mgoods where goods_MoreType_v=tb_mgoodsmoretype.gmt_Id_i) as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121)本回答被提问者采纳
(select count(*) from tb_mgoods where goods_MoreType_v=tb_mgoodsmoretype.gmt_Id_i) as counts
from tb_mgoodsmoretype where gmt_Id_i in(10,121)本回答被提问者采纳