简体   繁体   English

在MySQL中使用max(points)AS时如何获取完整行?

[英]How to get complete row when using max(points) AS in mysql?

I'm using this code. 我正在使用此代码。

$query = "SELECT max(Points) AS Points,City FROM Table_name WHERE Gender='Female' && Country='England' GROUP BY City";

I want it to give the complete row, not just Points and City. 我希望它给出完整的行,而不仅仅是“积分”和“城市”。 In my database the order is ID, City, Points, Name, Gender, Country. 在我的数据库中,顺序为ID,城市,积分,姓名,性别,国家/地区。 When I run this line of code it only gives me Points and City. 当我运行这一行代码时,它只会给我积分和城市。

I have tried * but doesn't work. 我已经尝试过*但没有用。

I guess you want the whole row with the max points value, ie rows with Female/England/city where there are no one with higher points: 我猜您想让整行具有最高分值,即具有雌性/英格兰/城市的行,其中没有一个具有更高的分值:

SELECT *
FROM Table_name t1
WHERE Gender='Female' 
 AND Country='England'
 AND NOT EXISTS (select 1 from Table_name t2
                 WHERE t2.Gender='Female' 
                   AND t2.Country='England'
                   AND t2.city = t1.city
                   AND t2.points > t1.points)

Will show both rows if it's a tie. 如果是平局将显示两行。

To return only one row / city, just add an < id condition in the EXISTS : 要仅返回一行/城市,只需在EXISTS添加<id条件:

SELECT *
FROM Table_name t1
WHERE Gender='Female' 
 AND Country='England'
 AND NOT EXISTS (select 1 from Table_name t2
                 WHERE t2.Gender='Female' 
                   AND t2.Country='England'
                   AND t2.city = t1.city
                   AND t2.points > t1.points
                   AND t2.id < t1.id)

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

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