※ 引述《villix (瓜子被蜀国的狗吃了)》之铭言:
: 不好意思请问一下,如果现在有两张table, table1有需要搜寻的字串,
: table2里面是有句子的字串,可以将table1的所有字串搜寻出来当作array
: 然后在table2里面搜寻有table1字串的句子吗?希望是可以将table1的所有
: 字串在table2里面做fulltext search 然后做count这样~~
大概是这样?
<?php
try {
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE 'keywords' ('keyword' TEXT);");
$pdo->exec("INSERT INTO 'keywords' ('keyword') VALUES ('google'),('bing'),('yahoo');");
$pdo->exec("CREATE TABLE 'messages' ('message' TEXT);");
$pdo->exec("INSERT INTO 'messages' ('message') VALUES ('how about google.com?'),('why not bing?'),('is yahoo good?');");
$pdo->exec("INSERT INTO 'messages' ('message') VALUES ('Captain America'),('Gundam Mark II'),('google AlphaGO');");
$result = $pdo->query("SELECT M.*, K.* FROM messages M
INNER JOIN (SELECT keyword FROM keywords) K
ON M.message LIKE '%' || K.keyword || '%'");
if ($result) {
foreach($result as $row) {
echo "found keyword [{$row['keyword']}] in message [{$row['message']}]\n";
}
}
$result = $pdo->query("SELECT COUNT(M.message) AS cnt, E.keyword AS keyword FROM messages M
INNER JOIN (SELECT keyword FROM keywords) E
ON M.message LIKE '%' || E.keyword || '%' GROUP BY E.keyword");
if ($result) {
foreach($result as $row) {
echo "the keyword [{$row['keyword']}] was founded in {$row['cnt']} messages\n";
}
}
} catch (PDOException $e) {
echo "Error!: {$e->getMessage()}\n";
}
执行结果:
$ php inner_join.php
found keyword [google] in message [how about google.com?]
found keyword [bing] in message [why not bing?]
found keyword [yahoo] in message [is yahoo good?]
found keyword [google] in message [google AlphaGO]
the keyword [bing] was founded in 1 messages
the keyword [google] was founded in 2 messages
the keyword [yahoo] was founded in 1 messages