Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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
Php Mysql组通过在列中显示总计值,然后再计算值,如何停止此操作?_Php_Mysql_Group By_Grouping - Fatal编程技术网

Php Mysql组通过在列中显示总计值,然后再计算值,如何停止此操作?

Php Mysql组通过在列中显示总计值,然后再计算值,如何停止此操作?,php,mysql,group-by,grouping,Php,Mysql,Group By,Grouping,我正在处理一个查询,以在多个列中找到重复的值,因此我将首先关注查询的单个部分,以获得更好的解释 在一天结束时,我需要知道的是,这4列中是否有一个重复,以及复制的列在哪个列中 以下是单个查询: select count(*) as cnt, 'CUST_REF' as what_column from sometable where status != 'whateverStatus' and custm_id = 1234 group by cust_ref having

我正在处理一个查询,以在多个列中找到重复的值,因此我将首先关注查询的单个部分,以获得更好的解释

在一天结束时,我需要知道的是,这4列中是否有一个重复,以及复制的列在哪个列中

以下是单个查询:

select  count(*) as cnt, 'CUST_REF' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1;
所以这很有效,除了输出是2行。看起来第一行是列中点击数>1的总数,然后下一行是实际重复数,如下所示:

cnt what_column
9440    CUST_REF
2   CUST_REF
我的问题是,在没有列总数的情况下,如何才能得到第二行?(此列的值2是正确的)即,我只想要:

cnt what_column    
2   CUST_REF
综合起来:

我将所有这些与一个
联合体
放在一起,因此对于4列,它是这样的:

select  count(*) as cnt, 'CUST_REF' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1
 union
 select  count(*) as cnt, 'CUST_PO' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_po having count(cust_po) > 1
  union
 select count(*) as cnt, 'SHIP_BL' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
  union
 select count(*) as cnt, 'CUST_SHIPID' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_shipid having count(cust_shipid) > 1;
它的输出呈现了下面的内容,我想把所有显示重复项的字段分组在一起,同时去掉总计数

cnt what_column
9440    CUST_REF
2   CUST_REF
332 CUST_PO
3   CUST_PO
2   CUST_PO
8   CUST_PO
4   CUST_PO
9   CUST_PO
37  CUST_PO
6   CUST_PO
5   CUST_PO
7   CUST_PO
11  CUST_PO
6609    SHIP_BL
2   SHIP_BL
5   SHIP_BL
8   SHIP_BL
3   SHIP_BL
4   SHIP_BL
6   SHIP_BL
7   SHIP_BL
9183    CUST_SHIPID
2   CUST_SHIPID
3   CUST_SHIPID
6   CUST_SHIPID
同样,在一天结束时,我需要知道的是,这4列中的任何一列都有一个副本,以及该副本所在的列

对于下面的注释,我无法共享表数据。但是让我们这样看,在将列添加回
HAVING
中的select之后:

select cust_ref as val, count(*) as cnt, 'CUST_REF' as what_column
     from sometable 
      where status != 'whateverStatus' 
        and custm_id = 1234
     group by cust_ref having count(cust_ref) > 1;
HAVING
中的所有列名都是此表中的实际列名,
what_列
只是一个别名,它显示了在哪个列/查询中找到了dupe

假设数据是这样的,我已经用*标记了前两列中的重复项。我希望它能让他们大胆:

id | cust_ref | cust_po | ship_bl |cust_shipid
997| **1234** | 9656    | 5656    | 9876
998| **1234** | **6353**| 2436    | 9394
999| 4327     | **6353**| 4388    | 4353
我很确定我最终会:

val cnt what_column
      3 CUST_REF
1234  2 CUST_REF

希望有帮助

您已经找到了重复项。因此,如果只需要不包含cnt列的列,则执行子查询:

select distinct what_column 
 from (
select  count(*) as cnt, 'CUST_REF' as what_column
from sometable 
 where status != 'whateverStatus' 
 and custm_id = 1234
group by cust_ref having count(cust_ref) > 1
union
 select  count(*) as cnt, 'CUST_PO' as what_column
 from sometable 
 where status != 'whateverStatus' 
  and custm_id = 1234
 group by cust_po having count(cust_po) > 1
union
 select count(*) as cnt, 'SHIP_BL' as what_column
from sometable 
 where status != 'whateverStatus' 
and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
union
select count(*) as cnt, 'CUST_SHIPID' as what_column
  from sometable 
where status != 'whateverStatus' 
and custm_id = 1234
group by cust_shipid having count(cust_shipid) > 1);

您对似乎是一个非常简单的问题的解释非常复杂,并且您还没有清楚地解释您想要计算为“重复”的内容-您想要一个值出现多次的总记录计数,还是一个值出现多次的计数

您将重复值的计数与域的计数混淆,从而进一步混淆了问题-恰好查询输出中的第二行是2-这不是您要查找的值,只是碰巧是相同的基数

此列的值2是正确的

这表明你想要后者。在这种情况下,因为:

select  cust_ref, count(*) as cnt, 'CUST_REF' as what_column
from sometable 
where status != 'whateverStatus' 
   and custm_id = 1234
group by cust_ref having count(cust_ref) > 1;
将给出前者,您只需要计算该查询输出的行数。您可以通过两种方式执行此操作:

SELECT COUNT(*) AS number_of_values_in_more_than_row, what_column
FROM (
   select  count(*) as cnt, 'CUST_REF' as what_column, cust_ref
   from sometable 
   where status != 'whateverStatus' 
      and custm_id = 1234
   group by cust_ref 
   having count(cust_ref) > 1
)
GROUP BY what_column
……或

select  count(DISTINCT cust_ref) as cnt, 'CUST_REF' as what_column
from sometable 
where status != 'whateverStatus' 
    and custm_id = 1234
group by cust_ref 
having count(DISTINCT cust_ref) > 1;

最终有效的答案是在外部查询中使用having子句,这将返回所需的正确数字:

SELECT sum(cnt) as dupes, COUNT(*) AS number_of_values_in_more_than_row, what_column
  FROM (
select  count(*) as cnt, 'CUST_REF' as what_column,cust_ref
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1
 union
 select  count(*) as cnt, 'CUST_PO' as what_column,cust_po
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_po having count(cust_po) > 1
  union
 select count(*) as cnt, 'SHIP_BL' as what_column,ship_bl
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
  union
 select count(*) as cnt, 'CUST_SHIPID' as what_column,cust_shipid
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_shipid having count(cust_shipid) > 1
 )x
 GROUP BY what_column having count(number_of_values_in_more_than_row) >0;

您应该为表架构添加一个清晰的数据样本和预期结果。您可以显示样本数据和表架构吗?我不明白你在这里想要实现什么。如果显示实际列值,
选择cust\u ref,count(*)作为cnt,
,您会得到什么?您是按未选择的字段分组的-这不是一件好事。请尝试在
选择sum(cnt),what\u column from(…)中将您的联合包装为按what\u column
分组的部分,你可能会发现每一行都有不同的cust_ref(例如),这将汇总所有特定类型的总数。伙计们,在阅读了你的评论之后,我用一些额外的信息更新了这个问题。希望对您有所帮助。因此,这将返回每个what_列。我怎么知道它是正确的?如果我在select子句中添加任何其他内容,我将返回额外的行,但是我如何确保它不会返回,因为值是9442,而不是2?您首先必须确定除了异常值之外,您可以获得的最多重复数。然后按计数过滤。因此,当count<11时(如果11是不包括异常值的最大重复数),您的上一个代码段没有产生任何结果,但上面的较大代码段给出的值比第94列CUST\U PO 2 CUST\U REF 10 CUST\U SHIPID 26 SHIP\U BL中的值多-这看起来正确!实际上,这并没有返回正确的结果,如果我从数据集中删除cust_ref的所有值(其中有1组重复项和1个唯一编号),因此将cust_ref字段中的所有值都置为空,则查询仍返回cust_ref 1,就像这个数字一样,列94 CUST\u PO 1 CUST\u REF 10 CUST\u SHIPID 26 SHIP\u BL中的值比行多,为什么它显示1,而列中根本没有值?