Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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_Select - Fatal编程技术网

用于查找不同行的Mysql查询显示不正确的结果

用于查找不同行的Mysql查询显示不正确的结果,mysql,select,Mysql,Select,我希望在表中找到不同记录的总数。 我有一个包含以下列的表 id, name, product, rating, manufacturer price 这有大约128行,其中一些是基于不同列名的重复行 我只想选择不同的行: select distinct name, product, rating, maufacturer, price from table 这将返回47行 出于分页目的,我需要找到不同记录的总数,因此我有另一个声明: select distinct count(name),

我希望在表中找到不同记录的总数。 我有一个包含以下列的表

id, name, product, rating, manufacturer price
这有大约128行,其中一些是基于不同列名的重复行

我只想选择不同的行:

select distinct name, product, rating, maufacturer, price from table
这将返回47行

出于分页目的,我需要找到不同记录的总数,因此我有另一个声明:

select distinct count(name), product, rating, maufacturer, price from table
但这将返回128而不是47


如何获取不同行的总数?任何帮助都将不胜感激。谢谢

尝试添加
按名称、产品、评级、制造商、价格分组
条款

您有不同的和相反的计数

SELECT COUNT(DISTINCT column_name) FROM table_name

另外,我会在计算时删除额外的字段,其他字段的结果将出乎意料。

这将需要运行实际查询两次。。。一个用于distinct的内部函数,然后将这些函数的计数作为一行返回,然后将其连接到原始select distinct

select distinct 
      t1.product, 
      t1.rating, 
      t1.maufacturer, 
      t1.price,
      JustTheCount.DistCnt
   from 
      table t1,
      ( select count(*) as DistCnt
           from ( select distinct      
                        t2.product, 
                        t2.rating, 
                        t2.maufacturer, 
                        t2.price 
                      from 
                         table t2 ) 
      ) JustTheCount

在以下查询中,由于
distinct
子句位于
name
列之前,因此您将获得具有不同名称的行:

SELECT DISTINCT name, product, rating, maufacturer, price FROM table
但是,要获取相同记录的计数,请使用以下格式:

SELECT COUNT(DISTINCT name) FROM table
请注意,
DISTINCT
位于
COUNT
函数的内部,以便计算不同的名称。您可能不希望在计数查询中包括其他列,因为它们将是集合中的随机样本。当然,如果你想要一个随机样本,那么就包括它们

大多数应用程序将首先运行count查询,然后运行查询以返回结果。还要记住,
COUNT(*)
只是一个估计值,其值可能与实际返回的记录数不同


选择DISTINCT COUNT(name),表中的产品在MySQL 4.x中甚至不是有效的查询。不能混合使用聚合列和非聚合列。在5.x中,它将运行,但非聚合列的值将是集合中的随机样本。

有可能在此处引发一些火焰。。您可以随时使用:

SQL\u CALC\u发现\u行作为SQL的第一部分。但这是非常具体的


不太清楚是要在同一个查询中获得结果计数,还是要运行不同的查询。两种解决方案都在这里。在“结果”的新列中:

select distinct name, product, rating, manufacturer, price, (
    select count(*) from (
        select distinct name, product, rating, manufacturer, price from table1
    ) as resultCount) as resultCount
from table1
请注意,前面的解决方案将对每行重复计数(*),这不是非常有效,甚至在视觉上也不吸引人。尝试运行两个查询,一个获取实际数据,另一个获取表中与该数据匹配的记录数:

select distinct name, product, rating, manufacturer, price from table1

select count(*) from (
    select distinct name, product, rating, manufacturer, price from table1
) as result

希望这有帮助

问题在于OP希望所有名称、产品等列都是不同的,而不仅仅是名称。这可能会减少47卢尔特。即使是一行,如果名称总是相同的,而另一个值又有什么变化,我总是好奇这样的问题,他们到底想做什么作为最终结果。
select distinct name, product, rating, manufacturer, price from table1

select count(*) from (
    select distinct name, product, rating, manufacturer, price from table1
) as result