简体   繁体   English

SQL选择查询排序的列

[英]SQL select columns ordered by a query

I have tried and googled everything I can think of but I couldn't find an answer I have this simple database 我已经尝试并搜索了所有可以想到的内容,但找不到答案,因为我有一个简单的数据库

technicien 技术人员

+------------------+------------+------+-----+
| Field            | Type       | Null | Key |
+------------------+------------+------+-----+
| employe id       | number(4)  | NO   | PRI |
| name             | char(11)   | NO   |     |
| salary           | int(11)    | NO   |     |
+------------------+------------+------+-----+

maintenance 保养

+------------------+------------+------+---------+
| Field            | Type       | Null | Key     |
+------------------+------------+------+---------+
| employe id       | number(4)  | NO   | foreign |
| IP               | char(11)   | NO   | foreign |
| maintenance_date | int(11)    | NO   |         |
+------------------+------------+------+---------+

pc 个人电脑

+------------------+------------+------+---------+
| Field            | Type       | Null | Key     |
+------------------+------------+------+---------+
| value            | number(4)  | NO   | foreign |
| IP               | char(11)   | NO   | PRI     |
| price            | int(11)    | NO   |         |
+------------------+------------+------+---------+

what I need is to show the name, id, and salary of every technicien who has done a maintenance ordered by the total number of maintenances effected. 我需要显示的是每个进行过maintenance technicien的姓名,身份证和薪水,并按进行的maintenance总数进行排序。

SELECT technicien.name, technicien.employeId, technicien.salary
FROM technicien
INNER JOIN  maintenance
ON maintenance.employeId = technicien.employeId
INNER JOIN pc
ON maintenance.IP = pc.IP
ORDER BY COUNT(maintenance.maintenance_date)

What you need is to GROUP ing with respect to the columns of table technicien , and ORDER ing by count of maintenance tasks : 您需要的是对表technicien的列进行GROUP ,并根据维护任务的数量进行ORDER

select t.*, count(0) cnt_maintenance
  from technicien t
  inner join maintenance m on ( t.employee_id = m.employee_id )
  inner join pc p on ( p.value = m.IP )
 group by t.employee_id, t.name, t.salary
 order by count(0);

SQL Fiddle Demo SQL小提琴演示

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

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