簡體   English   中英

MySQL PHP如果語句表中不存在來自表的關鍵字短語顯示結果

[英]MySQL PHP If Keyword Phrase From Table Does Not Exist In Sentence Table Show Results

我有兩個表:句子,否定句。

我想選擇Negatives.negphrase中不包含任何記錄的Sentences.sentence列。

句子中有20萬條記錄,否定詞中有5萬條記錄。

Sentences.sentence Sample Data
=============================

 - university lab on campus
 - laboratory designs
 - lab coats
 - math lab
 - methane production
 - meth lab

Negatives.negphrase Sample Data
======================================

 - coats
 - math lab
 - meth

Desired Result Set
==================

 - university lab on campus
 - laboratory designs
 - methane production

我嘗試使用另一個問題的結果,但是數據庫超時:

SELECT Sentences.sentence
FROM Sentences, Negatives
GROUP BY Sentences.sentence
HAVING (((Max(InStr(" " & sentence & " "," " & negphrase & " ")))=0));

我的答案

因此,我將為Richard b / c提供正確的答案,他的解決方案適用於較小的記錄集,但不適用於較大的記錄集。 這是我用來將所有否定關鍵字放入數組中的PHP代碼,然后使用UPDATE子句遍歷該數組以在Sentences表中標記新列'negmatch'。 我將在另一個WHERE子句中使用它來選擇Sentences.sentence WHERE negmatch <> 1。

我只需要對所有的短語運行一次此代碼,然后在添加其他關鍵字時,我使用相同的代碼,但是沒有循環來再次搜索句子(代碼未在下面顯示)。 該代碼需要6.5分鍾才能遍歷2800個UPDATE子句,因此初始加載相當長,但是一旦完成,就不必再次進行。

<?php
$mysqli = new mysqli("localhost", "myuser", "myuserpassword", "database");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if ($result = $mysqli->query("SELECT negphrase FROM negatives")) {  
    $row_cnt = $result->num_rows;
    printf("Negative keywords have %d rows.\n", $row_cnt); //print count of rows

    while($row = $result->fetch_assoc()){ //loop through all results by row
        foreach( $row  AS $value ) {
        $negative[] = $value;
    }
}


    /* free result set */
    $result->close();

    $data = array_values($negative); // get only values
    $data = array_filter($data);
    $datacount = 1;
    foreach($data as $val) { //loop through array to build MySQL WHERE clause


            $updatequery = "UPDATE Sentences SET negmatch=1 WHERE sentence REGEXP '[[:<:]]" . trim($val) . "[[:>:]]'";
            echo $updatequery  . "<br />";

            mysqli_query($mysqli,$updatequery) or die (mysqli_error($mysqli));
            echo $datacount . " " . trim($val) ."<br />";
            $datacount++;

        }

}
$mysqli->close();


    unset($result, $row, $mysqli,$value,$negative,$data,$val,$updatequery,$datacount,$row_cnt);

?>

SELECT t1.sentence FROM Sentences as t1
inner join Negatives as t2 on t1.sentence != t2.negphrase

確保兩個列都正確索引

使用否定左聯接,這將僅根據規則返回Senteces表中與否定表不匹配的行

select * from Sentences s 
left join Negatives n 
on (concat(" ",s.sentence," ") like concat("% ",n.negphrase," %"))
where n.negphrase is null

經過數據波紋管測試

CREATE TABLE IF NOT EXISTS `Negatives` (
  `negphrase` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `Negatives` (`negphrase`) VALUES
('coats'),
('math lab'),
('meth');

CREATE TABLE IF NOT EXISTS `Sentences` (
  `sentence` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `Sentences` (`sentence`) VALUES
('university lab on campus'),
('laboratory designs'),
('lab coats'),
('math lab'),
('methane production'),
('meth lab'),
('testing sentence');  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM