Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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 GROUP BY和ORDER BY_Php_Mysql_Sql_Mysqli - Fatal编程技术网

Php 对同一请求使用mysql GROUP BY和ORDER BY

Php 对同一请求使用mysql GROUP BY和ORDER BY,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我一直在尝试在同一条线上分组和点菜,以得到我想要的东西,但它不起作用。 我正在使用一个while循环,它运行数千个名字,检查每个城市的最高点。 如何在不重复同一个城市两次的情况下,从每个城市获得得分最高的名称 简而言之,这就是我的数据库中的内容: ID City Points Name 1 NYC 16 Stan 2 London 24 Paul 3 NYC 11 Jeffrey 4

我一直在尝试在同一条线上分组和点菜,以得到我想要的东西,但它不起作用。 我正在使用一个while循环,它运行数千个名字,检查每个城市的最高点。 如何在不重复同一个城市两次的情况下,从每个城市获得得分最高的名称

简而言之,这就是我的数据库中的内容:

ID City       Points    Name

1  NYC        16        Stan

2  London     24        Paul

3  NYC        11        Jeffrey

4  London     20        George

5  NYC        18        Ryan

$query = "SELECT `ID`, `City`, `Points`, `Name` FROM `table` GROUP BY `City` ORDER BY `Points`";
给我:

1 NYC 16 Stan

2 London 24 Paul
我希望它能给我什么:

2  London     24        Paul

5  NYC        18        Ryan
这是一个最常见的SQL问题之一。 你可以试试这样的

SELECT tab1.*
FROM @Table AS tab1
LEFT JOIN @Table AS tab2 
     ON tab1.City=tab2.city  AND tab2.points > tab1.points
WHERE tab2.City IS NULL;

您可以使用left join作为

select t1.* from table_name t1 
left join table_name t2 on t1.city=t2.city and t1.points < t2.points 
where t2.id is null;
检查这里的文件

或子查询

select t.* from table_name t 
where not exists (
  select 1 from test t1 where t.city = t1.city and t.points < t1.points
);

您需要使用HAVING和MAXpoints。我可以给你更多的信息,如果你准备一个SQLFIDLE的样本数据。非常感谢你,我的朋友!非常感谢你,Chakraborty先生!我最终使用了不相关的子查询。事实证明我更容易改变。它就像一个符咒:但如果我想应用WHERE-Gender='$male',它对这三个都不起作用。我该如何添加这些内容?应该在哪里工作,但这取决于您试图实现的目标。提供一些示例数据和所需结果。$query=select t.*从stad t join中选择maxpoints作为点,从stad组中选择城市,按x上的城市x。城市=t.city和x.points=t.points,其中性别='$SENDER';期望的结果与上面相同,但也检查性别,例如分类女性。
select t.* from table_name t 
where not exists (
  select 1 from test t1 where t.city = t1.city and t.points < t1.points
);