http://www.mail-archive.com/dbi-dev@perl.org/msg03906.html #### http://www.perlmonks.org/?node_id=284436 #### my ($sth, $query, $select, $from, $where, $likedate, $join, $selmin, $selmax, $min, $max, @sogs); $key =~ s/\'/\\\'/g; $select = qq/SELECT FLOOR( MIN( n.x ) ), FLOOR( MAX( n.x ) ) /; $from = qq/FROM o, n /; $where = qq/WHERE MATCH( o.owner, o.mn, o.manager ) AGAINST( '+$key' IN BOOLEAN MODE ) /; $likedate = qq/AND n.time LIKE '$date%' /; $join = qq/AND o.key = n.key /; $query = $select . $from . $where . $likedate . $join; $sth = $dbh->prepare( $query ); $sth->execute; $sth->bind_columns( $selmin, $selmax ); while( $sth->fetch ) { if ( !defined( $min ) || !defined( $max ) ) { $min = $selmin; $max = $selmax; } $min = $selmin if $selmin < $min; $max = $selmax if $selmax > $max; } my $countif = ""; for my $i ( $min .. $max ) { $countif .= qq/COUNT( IF( FLOOR( n.x ) = $i,1,null ) ) /; } $select = qq/SELECT $countif /; $from = qq/FROM o, n /; $where = qq/WHERE MATCH( o.owner, o.mn, o.manager ) AGAINST( '+$key' IN BOOLEAN MODE ) /; $likedate = qq/AND n.time LIKE '$date%' /; $join = qq/o.key = n.key /; $query = $select . $from . $where . $likedate . $join; $sth = dbh->prepare( $query ); $sth->execute; $sth->bind_columns( map {$_} @sogs ); while( $sth->fetchrow_arrayref ) { print "$key,"; for( 1 .. $min ) { print ","; } print join( ',', @sogs ), "\n"; } #### Can't DBI::st=HASH(0x1aa7a40)->bind_col(1, undef,...), need a reference to a scalar at /usr/lib/perl5/DBI.pm line 1867, line 56.