---
title: "mysql 有用的查询"
author: "luhantu"
date: "2018-04-20T17:32:55Z"
tags: ["MySQL"]
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2018-04-20T17:45:53Z"
id: 2419618
url: "https://luhantu.iteye.com/blog/2419618"
---

数据：

![](http://dl2.iteye.com/upload/attachment/0129/5966/ce2abab6-6111-3e18-ae8e-58c32ada92a3.png)

1.把分数最好的学生的成绩减去1分(更新某个最大值的项目)

```
UPDATE student a ,(SELECT MAX(score) score FROM student) b set a.score = a.score - 1 WHERE a.score = b.score;
```

 2.查询每个学生所有课程中分数最高的课程 （查询所有分组中某个值最大的项）

```
select a.name,a.className,a.score from (SELECT id,name,className,max(score) score from student s GROUP BY name) b
INNER JOIN student a on a.name = b.name where a.score = b.score
```