如何选择Distinct记录和计数总数与where子句

时间:2014-06-30 10:25:05

标签: mysql sql select group-by distinct

请帮助我如何使用where子句从表中选择和计算总不同值。

我的代码在这里..........

<? 

 $loc=mysql_query($che);
   while($loc288=mysql_fetch_array($loc)){

    $loc6=mysql_query("select distinct `location_name`, count(`location_name`) as CountOf from tb_job_location group by location_name");
    $loc5=mysql_fetch_array($loc6);

?>
This Query fetch result as

Delhi 1
Delhi 1

但它应该是

的结果

德里2

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

你正在同时使用distinct和group。

试试这个

   $loc6=mysql_query("select `location_name`, count(*) as CountOf 
                      from tb_job_location group by location_name ");

答案 1 :(得分:0)

GROUP BY 会返回DISTINCT记录,因此无需编写 DISTINCT 关键字。

试试这个:

SELECT `location_name`, COUNT(`location_name`) AS CountOfLocations 
FROM tb_job_location 
GROUP BY location_name;

答案 2 :(得分:0)

GROUP BY子句将在location_name上分组,返回location_name的数量。在这种情况下,您不需要DISTINCT子句。

SELECT `location_name`,
       count(`location_name`) AS CountOf
FROM tb_job_location
GROUP BY location_name