Edited at

メモ: Perl DBI mysql ROW_NUM

More than 1 year has passed since last update.

use DBI;

my $database = 'my_database';
my $host = 'localhost';
my $port = '3308';
my $db_user = 'user';
my $db_pass = 'pass';

my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $db_user, $db_pass);

my $sth = $dbh->prepare(
'SELECT @rownum:=@rownum+1 as ROW_NUM, TABLE_NAME '.
'FROM information_schema.TABLES A, '.
'(SELECT @rownum:=-1) B '.
'WHERE table_schema=database() '
);
$sth->execute;
for(my $i = 0; $i < $sth->rows; $i++){
my @row = $sth->fetchrow_array;
print "\t", $row[0], "\t", $row[1], "\n";
}
$sth->finish;
$dbh->disconnect;

参考ページ

MySQLのSELECT結果に行番号を振る。

SET @rownum := -1;使うと怒られるんじゃー!

C#だと"Allow User Variables=True"SETが使えるんだそうな。