sql min函数和其他列 [英] sql min function and other column

查看:217
本文介绍了sql min函数和其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在包含诸如min,max ...之类的聚合函数的select语句中添加另一列

示例:

SELECT user_id, MAX(salary) FROM users;

这条语句在sql标准中是正确的吗(在mysql中是正确的); 它在mysql中的工作,但是我想我读到某个地方,如果我将聚合函数放在select子句中,除了聚合函数,我什么也不能放,或者如果有group by,则分组列可以在select子句中(在mysql中)


编辑:

User(user_id, name, last_name, salary)

我想从User表中选择user_id, name, (maximum salary column);不用子查询就可以做到吗?

用户表

User_id, Name, Salary

| 1 | user1 | last1 | 500  |   |
|---|-------|-------|------|---|
| 2 | user2 | last2 | 1000 |   |
| 3 | user3 | last3 | 750  |   |
|   |       |       |      |   |

输出必须为user_id, username, lastname, and salary of the user who have the max salary,因此此处的输出必须为:

 2 user2 last2 1000


解决方案

告诉您有不同的解决方案,具体取决于您想要的....

没有分组依据,没有子查询,容易的蛋糕

select * 
from users
ORDER BY salary DESC
LIMIT 1

i want to know if it is possible to add another column to a select statement that contain an aggregate function like min, max ...

example :

SELECT user_id, MAX(salary) FROM users;

is this statement correct in the sql standard(in mysql its work ); its work in mysql, but i think i read somewhere that if i put an aggregate function in the select clause, i can't put anything but an aggregate function or if there is a group by, the grouped column can be in the select clause (in mysql)


EDIT :

User(user_id, name, last_name, salary)

i want to select the user_id, name, (maximum salary column) from the User table; is it possible to do it without sub query?

User Table

User_id, Name, Salary

| 1 | user1 | last1 | 500  |   |
|---|-------|-------|------|---|
| 2 | user2 | last2 | 1000 |   |
| 3 | user3 | last3 | 750  |   |
|   |       |       |      |   |

the output must be the user_id, username, lastname, and salary of the user who have the max salary, so here the ouput must be :

 2 user2 last2 1000


解决方案

Told you there are different solutions depending on what you want....

no group by, no subquery, Easy cake

select * 
from users
ORDER BY salary DESC
LIMIT 1

这篇关于sql min函数和其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆