简体   繁体   English

SELECT COUNT(DISTINCT名),ID,用户的地址

[英]SELECT COUNT(DISTINCT name), id, adress from users

With PHP I'm trying to run a SQL query and select normal columns as well as COUNT. 使用PHP,我试图运行SQL查询并选择普通列和COUNT。

$sql_str =  "select COUNT(DISTINCT name), id, adress from users";
$src = mysql_query($sql_str);

while( $dsatz = mysql_fetch_assoc($src) ){
    echo $dsatz['name'] . "<br>";
}

The problem is that when I have "COUNT(DISTINCT name)," in my query, it will only return the first entry. 问题是当我在查询中有“ COUNT(DISTINCT name)”时,它将仅返回第一个条目。 When I remove it, it will return all matching entries from the db. 当我删除它时,它将从数据库返回所有匹配的条目。

I could separate it and do 2 queries, but I'm trying to avoid this due to performance concerns. 我可以将其分开并进行2个查询,但是由于性能方面的原因,我试图避免这种情况。

What do I make wrong? 我怎么做错了? thx, Mexx 谢谢,Mexx

The ability to mix normal columns and aggregate functions is a (mis)feature of MySQL. 混合普通列和聚合函数的功能是MySQL的(错误)功能。 You can even read why it's so dangerous on MySQL's documentation: https://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html 您甚至可以在MySQL的文档中阅读为什么它如此危险: https//dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html

But if you really want to mix normal rows and a summary in a single query, you can always use the UNION statement: 但是,如果您确实想在单个查询中混合使用普通行和摘要,则可以始终使用UNION语句:

SELECT COUNT(DISTINCT name), null, null FROM users GROUP BY name --summary row
UNION
SELECT name, id, address FROM users --normal rows

COUNT() is an aggregate function, it aggregates against the results of the rest of your query. COUNT()是一个聚合函数,它根据其余查询的结果进行聚合。 If you want to count all distinct names, and not just the distinct names associated with the id and address that you are selecting, then yes, you will have to run two queries. 如果要计算所有唯一名称,而不仅仅是与您选择的idaddress相关联的唯一名称,那么是的,您将必须运行两个查询。 That's just how SQL works. 这就是SQL的工作方式。

Note that you should also have a group by clause when aggregating. 请注意,聚合时还应该有一个group by子句。 I think the fact that MySQL doesn't require it is horrible, and it encourages really bad habits. 我认为MySQL不需要它的事实太可怕了,它会养成真正的坏习惯。

From what I understand, you want to get : 据我了解,您想获得:

  • one line per user, to get each name/id/address 每个用户一行,以获取每个名称/ ID /地址
  • one line for several users at the same time, to get the number of users who have the same name. 同一行同时显示多个用户,以获取具有相同名称的用户数。

This is not quite possible, I'd say. 我会说这不太可能。


A solution would be, like you said, two queries... 如您所说,一个解决方案是两个查询...

... Or, in your case, you could do the count on the PHP side, I suppose. ...或者,就您而言,我想您可以在PHP方面进行计数。
ie, not count in the query, but use an additionnal loop in your PHP code. 即,不计入查询,而是在PHP代码中使用附加循环。

When you have a count() as part of the field list you should group the rest of the fields. 当count()作为字段列表的一部分时,应将其余字段分组。 In your case that would be 在你的情况下

select count(distinct name), id, adress from users group by id, adress
select count(distinct name), id, adress 
from users 
group by id, adress

I'm assuming you want to get all your users and the total count in the same query. 我假设您想在同一查询中获得所有用户和总数。

Try 尝试

  select name, id, address, count(id) as total_number 
    from users 
    group by name, id, address;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM