在我的 MYSQL 数据库公司中,我有一个表Worker
::
mysql> DESC `Worker`;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| SSN | varchar(64) | NO | | NULL | |
| name | varchar(64) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
我使用以下 MySQL 的命令创建:
CREATE TABLE `Worker` (
`SSN` varchar(64) NOT NULL,
`name` varchar(64) DEFAULT NULL,
`salary` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
该表具有以下数据:
mysql> SELECT * FROM `Worker` ORDER BY salary ASC;
+-----+---------+--------+
| SSN | name | salary |
+-----+---------+--------+
| 6 | TWO | 50 |
| 5 | ONE | 100 |
| 1 | Grijesh | 200 |
| 3 | Sumit | 250 |
| 2 | Rahul | 300 |
| 4 | Harsh | 500 |
| 7 | THREE | 1000 |
+-----+---------+--------+
7 rows in set (0.00 sec)
它的插入查询:
INSERT INTO `Worker`
(SSN, name, salary)
VALUES
("1", "Grijesh", 200),
('2','Rahul',300),
('3','Sumit',250),
('4','Harsh',500),
('5','ONE','100'),
('6','TWO',50),
('7','THREE',1000);
[问题]
我想选择第一个n workers
(在 asc 列表中)那些工资总和刚刚超过 500(> 500)。例如:
以下四名工人是第一低薪员工,工资总和为 600。(600 > 500)
| 6 | TWO | 50 |
| 5 | ONE | 100 |
| 1 | Grijesh | 200 |
| 3 | Sumit | 250 |
我需要SELECT * FROM
Worker 之类的东西WHERE SUM(salary) < 501 ASC;
(它的查询不正确)
可以假设所有人都有不同的薪水。
类似于削减成本:移除那些相对不重要的员工
阅读建议或链接会有所帮助