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
不明白为什么会出现如此的情况,请赐教!

解决方案 »

  1.   

    prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = "红南京"';
    execute stmt;orPREPARE stmt FROM 'select sum(cig_cnt) from cigstrinf where cig_type =\'红南京\'';
    EXECUTE stmt;
      

  2.   

    可能是字符集的问题两句话都放到存储过程里面一起执行看看
    select sum(cig_cnt) from cigstrinf where cig_type = '红南京';
    prepare stmt from 'select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''';
    execute stmt;
      

  3.   


    show variables like 'char%'; 
      

  4.   

    这个 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 = '红南京'";
      

  5.   

    set @sql='select sum(cig_cnt) from cigstrinf where cig_type = ''红南京''''
    prepare stmt from @sql;
    execute stmt;
      

  6.   

    表中字符集是怎样的。
    show variables like '%char%';
      

  7.   

    将两句话写入存储过程,调用存储过程之后的结果为:
    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
      

  8.   

    SET NAMES GBK
    再运行试试
      

  9.   

    set names gbk不就是把几个参数设置为gbk吗,我的那几个字符集设计已经是gbk格式啦