-1

我是 mysql 和 php 的新手,在http://answers.yahoo.com/question/index?qid=20080717110807AAsXkGL找到了类似的功能:

但是这段代码比较了 mysql 和 mysql:

第一个表 t1 中的 3 列:第一、最后、电子邮件。t2 中的相同列。

DELETE FROM t1 WHERE t1.email IN (SELECT t2.email from t2)

names但是,如果名称值存在于 A 列(名称)的 CSV 文件中,我需要一个 PHP 代码,该代码将从列中的 MySQL 表中删除整行。

在此先感谢您的帮助!我已经花了 4 个小时在网上寻找,但找不到解决方案。

4

2 回答 2

0

将结果的数组tmpArraySELECT t2.email from t2

并做这样的事情:

$in = implode(',', $tmpArray);

DELETE FROM t1 WHERE t1.email IN ($in)
于 2013-03-30T23:07:48.420 回答
0

以下是寻求者的完整答案:

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";

$dbname = "yourdatabase";

$link = mysql_connect($dbhost, $dbuser, $dbpass);

$lines =file('active.csv');
foreach($lines as $line){
$na[]=trim($line);
}

$in=implode(',',$na);


$query = "DELETE FROM table1 WHERE t2.value IN ($in)";
$del=mysql_query($query,$link);

If you want to delete in Table 1 entire row if the value is not present in CSV file then change the query to:

    $query = "DELETE FROM table1 WHERE t2.value NOT IN ($in)";

IMPORTANT NOTE: the header in CSV file should NOT be present because otherwise this script will delete entire Table 1.

于 2013-03-31T16:58:07.010 回答