使用PHP similar_text和UPDATE合并表

时间:2017-02-25 20:19:11

标签: php mysql sql

我在MySQL中有两个表我想合并:

table_1
--------------------------------------------------------
id |     table_1_name           |   table_1_art
--------------------------------------------------------
1  |     Ernest Hemingway       |   Writer
--------------------------------------------------------
2  |     Mark Twain             |   Writer
--------------------------------------------------------
3  |     Jackson Pollock        |   Painter
--------------------------------------------------------
4  |     Miles Davis            |   Musician
--------------------------------------------------------
5  |     Akira Kurosawa         |   Cinema
--------------------------------------------------------


table_2
--------------------------------------------------------
id |     table_2_name             |   table_2_art
--------------------------------------------------------
1  |     Hemingway, ernest        |   Writer
--------------------------------------------------------
2  |     Twain, Mark              |   Writer
--------------------------------------------------------
3  |     P., Jackson              |   Painter
--------------------------------------------------------
4  |     Miles Dewey Davis        |   Musician
--------------------------------------------------------
5  |     Bukowski, Charles        |   Poetry
--------------------------------------------------------

我想使用table_1_name的数据更新字段table_2_name,但前提是它们之间存在相似性。我正在使用similar_text,并选择超过40%的相似性:

<?php

// Connection
$server = "****";
$user = "****";
$pass = "****";
$db_name = "****";
$conn = new mysqli($server, $user, $pass, $db_name);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connected <br/>";
}

//Begin
$petition = "SELECT * FROM table_1,  table_2";
$result = mysqli_query($conn, $petition);
while ($row = mysqli_fetch_assoc($result)) {
    $table_1_name = $row['table_1_name'];
    $table_1_art = $row['table_1_art'];
    $table_2_name = $row['table_2_name'];
    $table_2_art =$row['table_2_art'];

//Ask for similarities
similar_text($table_1_name, $table_2_name, $percent);

//Show coincidences over 40%
if ($percent > "40%") {
    echo $table_1_name." — ".$table_2_name." — ".$percent."% <br />";

//Update field in table_2_name for this coincidences
$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'";
    if ($conn->query($sql) === true) {
        echo "updated <br/> <br/>";
    } else {
        echo "ERROR: " . $conn->error." <br/> <br/>";
    }
}
    $conn->close();
}

但我无法获得结果。而不是:

table_2
--------------------------------------------------------
id |     table_2_name             |   table_2_art
--------------------------------------------------------
1  |     Ernest Hemingway         |   Writer
--------------------------------------------------------
2  |     Mark Twain               |   Writer
--------------------------------------------------------
3  |     Jackson Pollock          |   Painter
--------------------------------------------------------
4  |     Miles Davis              |   Musician
--------------------------------------------------------
5  |     Bukowski, Charles        |   Poetry
--------------------------------------------------------

我明白了:

table_2
--------------------------------------------------------
id |     table_2_name             |   table_2_art
--------------------------------------------------------
1  |     Ernest Hemingway         |   Writer
--------------------------------------------------------
2  |     Ernest Hemingway         |   Writer
--------------------------------------------------------
3  |     Ernest Hemingway         |   Painter
--------------------------------------------------------
4  |     Ernest Hemingway         |   Musician
--------------------------------------------------------
5  |     Ernest Hemingway         |   Poetry
--------------------------------------------------------

我确定这是一个非常常见的错误,至少对于初学者来说,但我无法找到它!

尼基塔

PD:正如@denny所说,sql需要一个条件,我忘了说我尝试了一些选项,比如:

$sql = "UPDATE table_2 SET table_2_name = '$table_1_name' WHERE ($percent > '45%')";

现在的问题是:哪种情况和方式。不能相信那很难......

2 个答案:

答案 0 :(得分:0)

更改sql

$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'";

到此

$sql = "UPDATE table_2 SET table_2_name = '$table_1_name'" where column="your condition";

语法: -

update table_name set column_name = 'value' where column_name ='your_condition'

如果你执行

update table_name set column_name = 'value'

它将更新整个表格行

答案 1 :(得分:0)

好的,我找到了办法。

首先,我犯了一个错误:我正在while中间关闭连接:

$conn->close();
}

应该是最后的结果:

}
$conn->close();
?>

另外,我发现使用INSET INTO将de数据导出到第三个表比使用UPDATE第一个表更好。

首先我比较细胞:

$similar_text($table_1_name, $table_2_name, $percent);

然后将结果导出到table_3,为百分比添加一个字段:

if ($percent_autor > "40%") {
$sql = "INSERT INTO table_3 (table_3_name, table_3_art, percent)
VALUES ('$table_1_name', '$table_1_art, '$percent')";   
}

就是这样。最后会有一些带有重复数据的行,但很容易找到并删除它们;)