元素A表,字段a,b;
假设a,b两字段中共有100行数据,其中,b字段中有部分数据,有空值;而a字段中有也部分空数据;
求一SQL语句,怎么得到a,b字段都有数据的内容(排除空字段),以下为实际项目中存储过程拼接SQL语句:
BEGIN
declare a varchar(15);
declare b varchar(15);
declare A varchar(20);
SET @sql =concat('select id,',a,' as orig,',
b,' as tran from ',A,' where ',a is not null,'<>? or ',
b,'<>? limit ?,?;');
以上写法,空值和非空值都被查询出来了。
在线等,忘高人指点。

解决方案 »

  1.   

    表A;字段a,b都是变量哈。动态的。
      

  2.   

    示例,自行修改
    SET @sql =CONCAT('select id,',a,' as orig,', b,' as tran from A where ',@a,' is not null OR ', 
    @b,'IS NOT NULL LIMIT 1,2');
     SELECT @sql;
      

  3.   

    版主,报错:
    Incorrect arguments to EXECUTE
    执行不正确的参数。
      

  4.   

    你的代码,SELECT @ASQL内容是什么
      

  5.   

    写成这样不会报错,加了"<>?",但是得不到我要的结果,空值还是出来了。SET @sql =CONCAT('select id,',a,' as orig,', b,' as tran from A where ',@a,' is not null <>? OR ', 
    @b,'IS NOT NULL <>? LIMIT 1,2');
     SELECT @sql;继续求解。
      

  6.   

    SET @sql =CONCAT('select id,',a,' as orig,', b,' as tran from A where ',a,'  is not null and  ',b,' IS NOT NULL LIMIT 1,2');
      SELECT @sql;
      

  7.   

    完整的存储过程:
    CREATE DEFINER=`tmx`@`%` PROCEDURE `SelectALL`(in a varchar(10),
    in b varchar(10),in st int,in ed int,in A varchar(10))
    BEGIN
        declare a varchar(15);
    declare b varchar(15);
    declare A varchar(20);
    set a1=GetFieldName(a);
    set b1=GetFieldName(b);
    set stn=ChangeToSTableName(A);
        set @st=st,@ed=ed,@str='';
        SET @sql =concat('select id,',a1,' as orig,',
    b1,' as tran from ',stn,' where ',a1,' <>? OR ',
    b1,' <>? limit ?,?;');
        PREPARE stmt1 FROM @sql;
        EXECUTE stmt1 USING @str,@str,@st,@ed;
    END
      

  8.   

    @楼主:
    SET @sql =CONCAT('select id,',a,' as orig,', b,' as tran from A where ',a,'  is not null and  ',b,' IS NOT NULL LIMIT 1,2');
      SELECT @sql;
    这样也不对,还是报错:Incorrect arguments to EXECUTE
      

  9.   

    为什么加了“<>?”就不报错,得到了所有结果(为空和不为空)。加的这个表示什么含义啊?
      

  10.   

    你的MYSQL版本是什么? 之前的版本是不支持limit ?,?;');中使用变量为参数的。即LIMIT后不可以是变量。  SET @sql =concat('select id,',a1,' as orig,',
    b1,' as tran from ',stn,' where ',a1,' <>? OR ',
    b1,' <>? limit ',st,',',ed,';');
      

  11.   

    我的Mysql版本装的是:MySQL-server-5.5.29-1.rhel5.i386.rpm
      

  12.   

    直接拼接SQL,示例
    set a2='123';
    set a3='456';
    set st=3;
    set ed=4;
    SET @sql =concat('select id,',a1,' as orig,',
     b1,' as tran from ',stn,' where ',a1,' <>\'',a2,'\' OR ',
     b1,' <>\'',a3,' limit ',st,',',ed,';');
      

  13.   

    版主,发现一问题,在建A表时,创建的字段a和b为:VARCHAR(200); Default=''; Not null;就这3个参数;
      

  14.   

    这样在同过查询语句时,a,b两个字段视乎都是有值的,但是取到数据控件中,不是默认值的地方显示的是空的,我以为为空,实际上在数据库中是有个字符的('')。
    有点奇怪的是,我使用Workbench写sql查询语句,查询出来为空的地方就是看不到值。这个默认的''值该怎么排除啊。
      

  15.   

    拷贝有空值那一行的数据出来显示是这样的:'29', '', '全数字控制系统'  一次是id,a,b
      

  16.   

    在Workbench中执行sql语句:
    select id,a,b from A where a is not NULL OR b is not NULL;
    结果还是会得到有空值的数据。这就是问题所在了,看来并不是存储过程中Sql语句的问题。
      

  17.   

    create table A(
    id int not null auto_increment,
    a varchar(50) not null default '' comment '信息a', 
            b varchar(50) not null default '' comment '信息b',
    表是这么创建的。
      

  18.   

    select id,a,b from A where a is not NULL and b is not NULL;
      

  19.   

    是的,就是这么写的SQL语句,但是为空的值的确被查出来了。
    上面创建表的语句我没写完。完整的是下面的:
    create table A(
            id int not null auto_increment,
            a varchar(50) not null default '' comment '信息a', 
            b varchar(50) not null default '' comment '信息b',
            primary key (id),
    index(a), 
    index(b)
     ) engine=MyISAM AUTO_INCREMENT=1 default charset utf8 comment 'A表';
      

  20.   

    是NULL还是空值?
    select id,a,b from A where length(a)>0 and length(b)>0;