Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择“查询”可对项目进行分组,但也可限制_Mysql_Sql - Fatal编程技术网

Mysql 选择“查询”可对项目进行分组,但也可限制

Mysql 选择“查询”可对项目进行分组,但也可限制,mysql,sql,Mysql,Sql,我正在运行以下sql查询: SELECT * FROM customer_billing a WHERE (SELECT COUNT(*) FROM customer_billing b WHERE a.producttype = 'VoIP Telephony' and a.productname = 'VoIP Geographic Number (01/02)' and a.customer_seq = b.customer_seq) <= 1 order by

我正在运行以下sql查询:

SELECT * FROM customer_billing a WHERE 

(SELECT COUNT(*) FROM customer_billing b WHERE 
 a.producttype = 'VoIP Telephony' and 
 a.productname = 'VoIP Geographic Number (01/02)' and 
 a.customer_seq = b.customer_seq) 
 <= 1 order by customer_seq ASC
它应该只显示producttype='VoIP Telephony'所在的行,而显示其他两行


其中producttype='Phone Lines'等

您要做的是选择表中只有一行与给定producttype和productname匹配的客户。但是客户可以有更多的线路

一种解决方案是在外部选择上重复这两个条件:


我认为您正在尝试这样做:

SELECT *
FROM customer_billing cb
WHERE cb.producttype = 'VoIP Telephony' and 
      cb.productname = 'VoIP Geographic Number (01/02)' and
      (SELECT COUNT(*)
       FROM customer_billing cb2 
       WHERE cb2.producttype = cb.producttype and 
             cb2.productname = cb.productname and
             cb2.customer_seq = cb.customer_seq
     ) <= 1
order by customer_seq ASC;

您的查询版本正在子查询中进行比较。因此,它们只适用于计数*而不适用于整个查询的过滤。

为什么要进行计数*这是我所见过的where子句中最糟糕的子查询。用英语而不是代码,你能解释一下这个查询在做什么吗?
SELECT *
FROM customer_billing cb
WHERE cb.producttype = 'VoIP Telephony' and 
      cb.productname = 'VoIP Geographic Number (01/02)' and
      (SELECT COUNT(*)
       FROM customer_billing cb2 
       WHERE cb2.producttype = cb.producttype and 
             cb2.productname = cb.productname and
             cb2.customer_seq = cb.customer_seq
     ) <= 1
order by customer_seq ASC;