求一MySQL存储过程语句

huamao2007 2013-07-11 02:56:31
元素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 ?,?;');
以上写法,空值和非空值都被查询出来了。
在线等,忘高人指点。
...全文
262 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
楼主,这下终于对了,就是上面那写法,感谢楼主。
WWWWA 2013-07-11
  • 打赏
  • 举报
回复
是NULL还是空值? select id,a,b from A where length(a)>0 and length(b)>0;
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
是的,就是这么写的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表';
wwwwb 2013-07-11
  • 打赏
  • 举报
回复
select id,a,b from A where a is not NULL and b is not NULL;
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
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', 表是这么创建的。
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
在Workbench中执行sql语句: select id,a,b from A where a is not NULL OR b is not NULL; 结果还是会得到有空值的数据。这就是问题所在了,看来并不是存储过程中Sql语句的问题。
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
拷贝有空值那一行的数据出来显示是这样的:'29', '', '全数字控制系统' 一次是id,a,b
wwwwb 2013-07-11
  • 打赏
  • 举报
回复
怀疑你的字段名与变量名重复,修改试试
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
这样在同过查询语句时,a,b两个字段视乎都是有值的,但是取到数据控件中,不是默认值的地方显示的是空的,我以为为空,实际上在数据库中是有个字符的('')。 有点奇怪的是,我使用Workbench写sql查询语句,查询出来为空的地方就是看不到值。这个默认的''值该怎么排除啊。
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
版主,发现一问题,在建A表时,创建的字段a和b为:VARCHAR(200); Default=''; Not null;就这3个参数;
wwwwb 2013-07-11
  • 打赏
  • 举报
回复
直接拼接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,';');
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
楼主,13楼的答案,不行,看来我的问题把你难到了。13楼的执行效果报执行错误。
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
我的Mysql版本装的是:MySQL-server-5.5.29-1.rhel5.i386.rpm
ACMAIN_CHM 2013-07-11
  • 打赏
  • 举报
回复
你的MYSQL版本是什么? 之前的版本是不支持limit ?,?;');中使用变量为参数的。 即LIMIT后不可以是变量。 SET @sql =concat('select id,',a1,' as orig,', b1,' as tran from ',stn,' where ',a1,' <>? OR ', b1,' <>? limit ',st,',',ed,';');
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
版主,还在吗?完整代码我贴在8楼了,问题还没解决,求解啊。
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
为什么加了“<>?”就不报错,得到了所有结果(为空和不为空)。加的这个表示什么含义啊?
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
@楼主: 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
wwwwb 2013-07-11
  • 打赏
  • 举报
回复
SELECT @sql;
huamao2007 2013-07-11
  • 打赏
  • 举报
回复
完整的存储过程: 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
wwwwb 2013-07-11
  • 打赏
  • 举报
回复
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;
加载更多回复(6)

56,682

社区成员

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

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