mysql使用Execute和直接执行select语句结果不相同!

Diana_24 2012-06-18 11:00:13
mysql中使用execute语句如下
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''';
execute stmt;
结果为 null;
而直接执行select sum(cig_cnt) from cigstrinf where cig_type = '红南京';
能够得到正确的结果22
不明白为什么会出现如此的情况,请赐教!
...全文
3378 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Diana_24 2012-07-03
  • 打赏
  • 举报
回复
都没人搭理我的。。。
Diana_24 2012-06-19
  • 打赏
  • 举报
回复
set names gbk不就是把几个参数设置为gbk吗,我的那几个字符集设计已经是gbk格式啦
Rotel-刘志东 2012-06-18
  • 打赏
  • 举报
回复
表中字符集是怎样的。
show variables like '%char%';
Rotel-刘志东 2012-06-18
  • 打赏
  • 举报
回复
set @sql='select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''''
prepare stmt from @sql;
execute stmt;
nicenight 2012-06-18
  • 打赏
  • 举报
回复
这个 where 条件最后匹配的结果为空,
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''';

改成如下试试:
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = "红南京"';

prepare stmt from "select sum(cig_cnt) from cigstrinf where cig_type = '红南京'";
wwwwb 2012-06-18
  • 打赏
  • 举报
回复

show variables like 'char%';
rucypli 2012-06-18
  • 打赏
  • 举报
回复
可能是字符集的问题

两句话都放到存储过程里面一起执行看看
select sum(cig_cnt) from cigstrinf where cig_type = '红南京';
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''';
execute stmt;
wwwwb 2012-06-18
  • 打赏
  • 举报
回复
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = "红南京"';
execute stmt;

or

PREPARE stmt FROM 'select sum(cig_cnt) from cigstrinf where cig_type =\'红南京\'';
EXECUTE stmt;
ACMAIN_CHM 2012-06-18
  • 打赏
  • 举报
回复
你表的这个字段的字符集设置是什么?
wwwwb 2012-06-18
  • 打赏
  • 举报
回复
SET NAMES GBK
再运行试试
Diana_24 2012-06-18
  • 打赏
  • 举报
回复
将两句话写入存储过程,调用存储过程之后的结果为:
sum(cig_amount)
22
sum(cig_amount)
NULL
我觉得可能是字符集的问题,但是两种查询方式都用了汉字,为什么结果还会不一样呢?
show variables like '%character_set%'结果为:
client gbk
connection gbk
database gbk
filesystem gbk
results gbk
server gbk
system utf8

表中cig_amount字段的Collation设置为NULL
cig_type字段的Collation设置为gbk_chinese_ci


56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧