元素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 ?,?;');
以上写法,空值和非空值都被查询出来了。
在线等,忘高人指点。
假设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 ?,?;');
以上写法,空值和非空值都被查询出来了。
在线等,忘高人指点。
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;
Incorrect arguments to EXECUTE
执行不正确的参数。
@b,'IS NOT NULL <>? LIMIT 1,2');
SELECT @sql;继续求解。
SELECT @sql;
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
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
b1,' as tran from ',stn,' where ',a1,' <>? OR ',
b1,' <>? limit ',st,',',ed,';');
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,';');
有点奇怪的是,我使用Workbench写sql查询语句,查询出来为空的地方就是看不到值。这个默认的''值该怎么排除啊。
select id,a,b from A where a is not NULL OR b is not NULL;
结果还是会得到有空值的数据。这就是问题所在了,看来并不是存储过程中Sql语句的问题。
id int not null auto_increment,
a varchar(50) not null default '' comment '信息a',
b varchar(50) not null default '' comment '信息b',
表是这么创建的。
上面创建表的语句我没写完。完整的是下面的:
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表';
select id,a,b from A where length(a)>0 and length(b)>0;