select distinct (`hostname`) as h from `drpl_comment` c where
(select count(cid) from `drpl_comment` where hostname == h ) > 0
如果主机名值在表格中出现多次,我的目的是从drpl_comment表中获取所有主机名。
你能帮我构建一下这个查询吗?
答案 0 :(得分:3)
您只需使用GROUP BY
和HAVING
SELECT hostname
FROM drpl_comment
GROUP BY hostName
HAVING COUNT(cid) > 0
答案 1 :(得分:0)
试试这个
SELECT DISTINCT `hostname` AS h, COUNT(`hostname`) AS count_h
FROM `drpl_comment`
GROUP BY `hostname`
HAVING count_h > 2
GROUP BY ... HAVING
解决了这个问题:)
如 J W 所述,使用DISTINCT
不会影响此上下文中的查询结果。所以它可以从查询中删除。