2015-04-26 39 views

回答

0

這個工作對我來說:

SELECT `description`, COUNT(`description`) FROM `items` WHERE `description` IN ("keyword1","...","keyword1000") GROUP BY `description` 

這裏的關鍵因素是IN元素,它允許specifiy在WHERE命令爲同一列過濾幾個項目。

你能拿到1000個項目與用雙引號(「)分隔每個項目的字符串和一個逗號(,)如果是,粘貼到IN命令

警告:?我沒有試過如果發生這種情況,請研究max_allowed_packet設置或考慮將查詢拆分爲幾個較小的查詢

0

您可以將您的查詢和然後運行1000次:

或者您可以加載您的1 000字表中並運行一個連接查詢:

$sql = ''; 
$text = file_get_contents('keywords.txt'); 
foreach (explode($text, PHP_EOL) as $keyword) 
    $sql .= "INSERT INTO tmpText (keyword) VALUES ('$keyword'); "; 
if (mysqli_multi_query($conn, $sql)) { 
    echo "New records created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 
// Now run this query: 
// SELECT keyword, COUNT(description) AS Matches 
// FROM tmpText, items 
// WHERE description LIKE CONCAT('%',keyword,'%') 
// GROUP BY keyword 
0

你必須先進口所有關鍵字到一個表的列,然後相互匹配這兩個表的列。

首先導入文件的關鍵字爲表:

LOAD DATA INFILE 'mydata.txt' INTO TABLE table_mydata 
    FIELDS TERMINATED BY '' 
    LINES TERMINATED BY '\r\n' (column_name); 

現在,像這樣的兩個表的匹配列:

SELECT 
    i.description, 
    count(m.column_name) 
    FROM items i 
    JOIN table_mydata m 
    ON i.description LIKE CONCAT('%', m.column_name, '%') 
    group by i.description; 
相關問題