这个查询有什么问题?

时间:2012-08-08 00:28:44

标签: mysql

我有一个包含以下字段的表:id |域名| domain_certificate_no |键值

select语句输出的示例可以是:

'57092', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_1', '55525772666'
'57093', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_2', '22225554186'
'57094', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_3', '22444356259'
'97168', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_1', '55525772666'
'97169', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_2', '22225554186'
'97170', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_3', '22444356259’

我想编写一个查询来检索重复keyvalue的数量,以及与多个keyvalue与cert _1相关联的domain_certificate_no。所以我写了这个:

select count(keyvalue), keyvalue from db.table group by (keyvalue) 
having count(keyvalue)>1 and domain_certificate_no like '%_1';

我收到错误号1054,说明未知列domain_certificate_no。你能告诉我出了什么问题吗?如何编写查询以实现我的目的?

3 个答案:

答案 0 :(得分:1)

关于逻辑:

SELECT COUNT(domain_certificate_no), keyvalue
FROM db.table 
WHERE domain_certificate_no LIKE '%_1'
GROUP BY keyvalue
HAVING COUNT(keyvalue)>1;

如果仍有您的未知列错误,请显示DESC db.table;

的结果

干杯。

答案 1 :(得分:1)

LIKE关键字应位于您的WHERE子句

   SELECT domain_certificate_no, COUNT(keyvalue) 
     FROM db.table 
    WHERE domain_certificate_no LIKE '%_1'
 GROUP BY domain_certificate_no
   HAVING COUNT(keyvalue) > 1;

答案 2 :(得分:0)

SELECT
    COUNT(keyvalue) AS keyvaluecount, keyvalue 
FROM 
    db.table 
HAVING
    keyvaluecount > 1 AND domain_certificate_no LIKE '%_1' 
GROUP BY 
    (keyvalue)