您嘗試的查詢是解決方案的一部分。將該查詢用作內聯視圖,以標識具有多個着陸頁的關鍵字。將該查詢的結果返回到原始表格。
SELECT t.keyword
, t.landing_page
FROM (-- keyword with more than one landing page
SELECT r.keyword
FROM search_data r
GROUP BY r.keyword
HAVING COUNT(DISTINCT r.landing_page) > 1
) s
JOIN search_data t
ON t.keyword = s.keyword
GROUP BY t.keyword, t.landing_page
ORDER BY t.keyword, t.landing_page
這不是唯一的方法。還有其他查詢模式也會返回相同的結果。作爲另一種方法的例子,使用一個相關子查詢以檢查另一行的表中是否存在具有相同的關鍵字,但不同的LANDING_PAGE:
SELECT DISTINCT t.keyword, t.landing_page
FROM search_data t
WHERE EXISTS (SELECT 1
FROM search_data r
WHERE r.keyword = t.keyword
AND NOT (r.landing_page <=> t.landing_page)
)
ORDER BY t.keyword, t.landing_page
隨訪
示範設置:
CREATE TABLE search_data (keyword VARCHAR(10), landing_page VARCHAR(80))
;
CREATE INDEX search_data_IX1 ON search_data (keyword, landing_page)
;
INSERT INTO search_data (keyword, landing_page) VALUES
('orange','https://www.example.co.uk/')
,('apple','https://www.example.co.uk/')
,('pear','https://www.example.co.uk/')
,('apple','https://www.example.co.uk/')
,('apple','https://www.example.co.uk/landing-page')
;
EXPLAIN查詢1
EXPLAIN
SELECT t.keyword
, t.landing_page
FROM (-- keyword with more than one landing page
SELECT r.keyword
FROM search_data r
GROUP BY r.keyword
HAVING COUNT(DISTINCT r.landing_page) > 1
) s
JOIN search_data t
ON t.keyword = s.keyword
GROUP BY t.keyword, t.landing_page
ORDER BY t.keyword, t.landing_page
-- id select_type table type possible_keys key key_len ref rows Extra
-- ------ ----------- ---------- ------ --------------- --------------- ------- ------ ------ ------------------------
-- 1 PRIMARY <derived2> system (NULL) (NULL) (NULL) (NULL) 1
-- 1 PRIMARY t ref search_data_IX1 search_data_IX1 13 const 2 Using where; Using index
-- 2 DERIVED r index (NULL) search_data_IX1 96 (NULL) 5 Using index
執行查詢1
SELECT t.keyword
, t.landing_page
FROM (-- keyword with more than one landing page
SELECT r.keyword
FROM search_data r
GROUP BY r.keyword
HAVING COUNT(DISTINCT r.landing_page) > 1
) s
JOIN search_data t
ON t.keyword = s.keyword
GROUP BY t.keyword, t.landing_page
ORDER BY t.keyword, t.landing_page
-- keyword landing_page
-- ------- --------------------------------------
-- apple https://www.example.co.uk/
-- apple https://www.example.co.uk/landing-page
解釋查詢2
EXPLAIN
SELECT DISTINCT t.keyword, t.landing_page
FROM search_data t
WHERE EXISTS (SELECT 1
FROM search_data r
WHERE r.keyword = t.keyword
AND NOT (r.landing_page <=> t.landing_page)
)
ORDER BY t.keyword, t.landing_page
-- id select_type table type possible_keys key key_len ref rows Extra
-- ------ ------------------ ------ ------ --------------- --------------- ------- -------------- ------ -------------------------------------
-- 1 PRIMARY t range (NULL) search_data_IX1 96 (NULL) 6 Using where; Using index for group-by
-- 2 DEPENDENT SUBQUERY r ref search_data_IX1 search_data_IX1 13 test.t.keyword 1 Using where; Using index
執行查詢2
SELECT DISTINCT t.keyword, t.landing_page
FROM search_data t
WHERE EXISTS (SELECT 1
FROM search_data r
WHERE r.keyword = t.keyword
AND NOT (r.landing_page <=> t.landing_page)
)
ORDER BY t.keyword, t.landing_page
-- keyword landing_page
-- ------- --------------------------------------
-- apple https://www.example.co.uk/
-- apple https://www.example.co.uk/landing-page
不要指望你的HAVING子句 –
@TheOneandOnlyChemistryBlob爲什麼不分開? – Barmar
@Barmar,爲什麼不選擇DISTINCT來減少要過濾的數據量?如果你在你的子句中區分數據可能已經在此點之前被過濾了 –