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
不明白为什么会出现如此的情况,请赐教!
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
不明白为什么会出现如此的情况,请赐教!
execute stmt;orPREPARE stmt FROM 'select sum(cig_cnt) from cigstrinf where cig_type =\'红南京\'';
EXECUTE stmt;
select sum(cig_cnt) from cigstrinf where cig_type = '红南京';
prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''';
execute stmt;
show variables like 'char%';
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 = '红南京'";
prepare stmt from @sql;
execute stmt;
show variables like '%char%';
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
再运行试试