1
假設下面的查詢:防止出現在結果計算列設置
SELECT meta_value as xyz, COUNT(*) AS count
FROM wp_postmeta
WHERE meta_key = "xyz" AND meta_value != ""
GROUP BY meta_value
HAVING count > 1
查詢工作正常,但我不需要在我的結果count
列。如何排除count
列?
我知道我可以使用子查詢(如下所示),但我想知道是否有更好的方法。
SELECT xyz FROM (
SELECT meta_value as xyz, COUNT(*) AS count
FROM wp_postmeta
WHERE meta_key = "xyz" AND meta_value != ""
GROUP BY meta_value
HAVING count > 1
) AS t
'HAVING count> 1'正在使用select中的別名計數...因此,您可以使用count(*)而不是HAVING子句中的別名,並從select中消除count(*)和別名。因此它變得像'SELECT meta_value as xyz ... HAVING count(*)> 1' – xQbert
這個問題的第一個答案應該可以解決你的問題:[stackoverflow.com/questions/7151401/...](https:/ /stackoverflow.com/questions/7151401/sql-query-for-finding-records-where-count-1)。將'COUNT(*)'移到你的'HAVING'子句... – War10ck
@xQbert將它添加爲答案 – Shadow