LoginSignup
1
1

More than 5 years have passed since last update.

メモ: Perl DBI mysql ROW_NUM

Last updated at Posted at 2016-12-15
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が使えるんだそうな。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1