好的,如果您没有要在表上检查的关键字,那么可悲的是,您正在查看这样的查询:
SELECT CASE WHEN urlCol LIKE '%Url-Title-1%' THEN 'Url-Title-1'
WHEN urlCol LIKE '%Url-Title-2%' THEN 'Url-Title-2'
WHEN urlCol LIKE '%Url-Title-3%' THEN 'Url-Title-3'
WHEN urlCol LIKE '%Url-Title-4%' THEN 'Url-Title-4'
..... END Url,
COUNT(DISTINCT userId) TotalVisitors
FROM someTable
WHERE urlCol LIKE '%Url-Title-1%'
OR urlCol LIKE '%Url-Title-2%'
OR urlCol LIKE '%Url-Title-3%'
OR urlCol LIKE '%Url-Title-4%'
.....
GROUP BY CASE WHEN urlCol LIKE '%Url-Title-1%' THEN 'Url-Title-1'
WHEN urlCol LIKE '%Url-Title-2%' THEN 'Url-Title-2'
WHEN urlCol LIKE '%Url-Title-3%' THEN 'Url-Title-3'
WHEN urlCol LIKE '%Url-Title-4%' THEN 'Url-Title-4'
..... END
现在,如果您可以将这些关键字放在一个表中,您的查询将是这样的:
SELECT B.KeyWord,
COUNT(DISTINCT userId) TotalVisitors
FROM someTable A
INNER JOIN TableWithKeywords B
ON A.urlCol LIKE '%' + B.KeyWord + '%'
GROUP BY B.KeyWord
有很大的不同,对吧?而且您可以在该表上添加或删除关键字,而无需触及您的查询,非常方便。