[英]Compare two sql tables
<?php
// Get $TableA from TableA and $TableB from Table B (std code)
$result = array();
foreach ($TableA as $id => $string) {
$result[$id] = array();
$words = explode(' ', $string);
foreach ($words as $word) {
if (in_array($word, $TableB)) {
$result[$id][] = $word;
}
}
}
// - display $result - left as exercise!
考慮在聚合查詢中使用CROSS JOIN
的純SQL解決方案,計算字符串和單詞之間的LIKE
實例。 下面使用條件SUM
聚合計算總數,並使用GROUP_CONCAT
輸出逗號分隔列表中的單詞:
SELECT t1.id, SUM(CASE WHEN t1.string LIKE CONCAT('%', t2.words, '%')
THEN 1
ELSE 0
END) AS WordCount,
GROUP_CONCAT(CASE WHEN t1.string LIKE CONCAT('%', t2.words, '%')
THEN t2.words
ELSE NULL
END) AS Words
FROM Table1 t1
CROSS JOIN Table2 t2
GROUP BY t1.id
請參閱rextester演示 。
警告: CROSS JOIN
會降低大型工作台的性能,因為笛卡爾產品(所有可能的組合配對)在兩組之間運行,可以非常快速地運行數百萬。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.