0
select distinct (`hostname`)  as  h from `drpl_comment` c where 
(select count(cid) from  `drpl_comment` where  hostname == h ) > 0

如果主机名值在表中出现多次,我的意图是从 drpl_comment 表中获取所有主机名。

你能帮我建立这个查询吗?

4

2 回答 2

3

你可以简单地使用GROUP BYHAVING

SELECT  hostname
FROM    drpl_comment
GROUP   BY hostName
HAVING  COUNT(cid) > 0
于 2013-04-12T18:38:26.540 回答
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不会影响查询的结果。所以它可以从查询中删除。

于 2013-04-12T18:42:52.680 回答