获取mysql中受影响的行数

wlzqhblo  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(427)

正在尝试获取包含250k条记录的受影响行返回0文件:

$affectedRows = 0;
$affectedRows = $pdo->exec(
        "DELETE FROM tablename WHERE Col3 BETWEEN '2018-01-01 00:00:00' AND '2018-01-31 00:00:00';
        ALTER TABLE tablename AUTO_INCREMENT = 1;
        LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)");

echo var_dump($affectedRows);

存储csv,但返回0

cgyqldqp

cgyqldqp1#

解决方法:
完全支持此脚本:

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)

但我在一些查询中把它和其他的合并在一起:

DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';
ALTER TABLE tbalename AUTO_INCREMENT = 1;
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
    FIELDS TERMINATED BY ',' 
    LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);

这是错误的,因为 $PDO->exec() 无法返回受影响的表或行数。
相反,要解决我需要一个数组,因为我的脚本支持这样:

$stmtpre[1] = "DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';";
$stmtpre[2] = "ALTER TABLE tbalename AUTO_INCREMENT = 1;";
$stmtpre[3] = "LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);";

与此相同:https://stackoverflow.com/a/52370701/9632001

相关问题