select distinct (`hostname`) as h from `drpl_comment` c where
(select count(cid) from `drpl_comment` where hostname == h ) > 0
如果主机名值在表中出现多次,我的意图是从 drpl_comment 表中获取所有主机名。
你能帮我建立这个查询吗?
你可以简单地使用GROUP BY
和HAVING
SELECT hostname
FROM drpl_comment
GROUP BY hostName
HAVING COUNT(cid) > 0
尝试这个
SELECT DISTINCT `hostname` AS h, COUNT(`hostname`) AS count_h
FROM `drpl_comment`
GROUP BY `hostname`
HAVING count_h > 2
GROUP BY ... HAVING
正好解决这个问题:)
正如JW所提到的,在这种情况下,使用DISTINCT
不会影响查询的结果。所以它可以从查询中删除。