免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2488 | 回复: 9
打印 上一主题 下一主题

高分求救.PERL简单的文件及数据库操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-29 12:11 |只看该作者 |倒序浏览
5可用积分
sub get_ci_sz{
        my @ci_sz;
        my @ne_id_sz;
        my $sql_sz="select count(*) a,c.ci from unicom:hb_cm_cell a ,unicom:ne_bsc b ,unicom:ne_cell c where a.type1=b.china_name and c.city_id=b.city_id and c.ci=a.ci   and b.city_id='114' and c.n_confirm<>1 group by 2 having count(*)>1";
        $sth=$dbh->prepare($sql_sz);
        $sth->execute();
        while(my $line=$sth->fetchrow_array){
                @ci_sz=$line;
                my $s="select ne_id from ne_cell where n_confirm<>1 and ci=@ci_sz and city_id='114' order by 1";
                $sth=$dbh->prepare($s);
                $sth->execute();
                while(my @row=$sth->fetchrow_array){
                print $row[0],"\n";
                }
        }
        }

描述:本想将第一个SQL查询结果@ci_sz作为第二个SQL查询的条件.但是从打印的结果来看.引入的参数只是第一个SQL的最后一个值.也就是说:只是@CI_SZ的最后一个数值.数组中的其它数值并没有引入到第二个查询SQL中,请高手帮忙.

论坛徽章:
0
2 [报告]
发表于 2008-11-29 13:02 |只看该作者
简单的调试一下嘛,你自己试试

论坛徽章:
0
3 [报告]
发表于 2008-11-29 20:44 |只看该作者
while(my @line=$sth->fetchrow_array){
                my $s="select ne_id from ne_cell where n_confirm<>1 and ci=$line[1] and city_id='114' order by 1";
                $sth=$dbh->prepare($s);
                $sth->execute();
                while(my @row=$sth->fetchrow_array){
                print $row[0],"\n";
                }

[ 本帖最后由 dl0622 于 2008-11-29 20:50 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-11-30 08:28 |只看该作者
@ci_sz=$line; 有毛病

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
5 [报告]
发表于 2008-11-30 13:47 |只看该作者
应该是这样,觉得

push @ci_sz, $line;

论坛徽章:
0
6 [报告]
发表于 2008-11-30 14:47 |只看该作者

回复 #5 wxlfh 的帖子

效果是一样的

论坛徽章:
0
7 [报告]
发表于 2008-11-30 14:47 |只看该作者

回复 #3 dl0622 的帖子

还是一样的,

论坛徽章:
0
8 [报告]
发表于 2008-11-30 15:51 |只看该作者
你要搞清楚,$sth->execute();执行后得到数组保存的是select语句中select的那几个数值,你下面用到的应该是这两个数值count(*) a,c.ci 的c.ci吧。

你的第一条sql语句select出来的结果,应该有很多行吧?因为最后得到的数组只保存一行,那么就是保存最后一行了(最后一行最迟select出来嘛)

你若想得到多行中每行的c.ci值,在第一个sql语句中加入能唯一确定该行值的字段,该字段赋一个值$x,然后在外面加一个循环,例如while($x<100),每通过一个循环得到一个值后就把它push进一个数组,然后供下面第二个sql语句调用。

不知道是否看得明白?

[ 本帖最后由 dl0622 于 2008-11-30 15:59 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-11-30 16:11 |只看该作者


  1. sub get_ci_sz{
  2.         my @ci_sz;
  3.         my @ne_id_sz;
  4.         for($x=1;$x<100;$x++){
  5.                 my $sql_sz="select count(*) a,c.ci from unicom:hb_cm_cell a ,uni
  6. com:ne_bsc b ,unicom:ne_cell c where c.ci=$x and a.type1=b.china_name and c.city_id=b.city_i
  7. d and c.ci=a.ci   and b.city_id='114' and c.n_confirm<>1 group by 2 having count
  8. (*)>1";
  9.                 $sth=$dbh->prepare($sql_sz);
  10.                 $sth->execute();
  11.                 @line=$sth->fetchrow_array;
  12.                 push @a,$line[1];
  13.         }
  14.         foreach $t (@a){
  15.                 my $s="select ne_id from ne_cell where n_confirm<>1 and ci=$t an
  16. d city_id='114' order by 1";
  17.                 $sth=$dbh->prepare($s);
  18.                 $sth->execute();
  19.                 while(my @row=$sth->fetchrow_array){
  20.                         print $row[0],"\n";
  21.                 }
  22.         }
  23. }

复制代码

[ 本帖最后由 dl0622 于 2008-11-30 16:13 编辑 ]

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
10 [报告]
发表于 2008-11-30 18:38 |只看该作者
原帖由 HF.SKY000 于 2008-11-29 12:11 发表
sub get_ci_sz{
        my @ci_sz;
        my @ne_id_sz;
        my $sql_sz="select count(*) a,c.ci from unicom:hb_cm_cell a ,unicom:ne_bsc b ,unicom:ne_cell c where a.type1=b.china_name and c.city_id=b.city_id and ...

while(my $line=$sth->fetchrow_array)  用数组
perldoc DBI
fetchrow_array
If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that


另外

$sth=$dbh->prepare($sql_sz);
        $sth->execute();  #select 1
        while(my $line=$sth->fetchrow_array){ #最好用@line
                @ci_sz=$line;
                my $s="select ne_id from ne_cell where n_confirm<>1 and ci=@ci_sz and city_id='114' order by 1";
                $sth=$dbh->prepare($s);
                $sth->execute();   #执行后,select 1的结果被覆盖了
                while(my @row=$sth->fetchrow_array){
                print $row[0],"\n";
                }
        }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP