简体   繁体   English

MySQL表中不同值的计数未正确添加

[英]Count of different values in MySQL table not adding correctly

I am trying to get a count of the number of different countries I have in a table but can't get the right result when I echo it out with php. 我试图获取表中不同国家的数量,但是当我用php回显它时却无法获得正确的结果。 although phpmyadmin gives the correct answer. 虽然phpmyadmin提供了正确的答案。 This is my query: 这是我的查询:

$sql2 = "SELECT count(*) as country FROM tpf_parks GROUP BY country" ;
$result2 = $pdo->query($sql2);
foreach ($result2 as $row2)

and this is how I am diplaying it 这就是我玩的方式

echo $row2[ 'country' ]

On if I run the query on phpmyadmin I get the correct answer of 17. When run through the above php it returns 56. What have I done wrong? 如果我在phpmyadmin上运行查询,将得到正确的答案17。通过上述php运行时,它返回56。我做错了什么?

If your question is described correctly and you're looking for the number of different countries you have, the query seems wrong. 如果您的问题描述正确,并且您正在寻找拥有的不同国家/地区的数量,则该查询似乎是错误的。 The query you wrote should give you a row per country, and the value for each row is the number of tpf_parks rows with that country. 您编写的查询应为每个国家/地区提供一行,并且每行的值是该国家/地区的tpf_parks行数。

The query I would use to determine the number of different countries mentioned in the country column of the tpf_parks table: 我将使用该查询来确定tpf_parks表的国家/地区列中提到的不同国家/地区的数量:

SELECT COUNT(DISTINCT country) FROM tpf_parks

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

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