SQL选择查询排序的列

时间:2018-06-24 00:42:56

标签: sql oracle

我已经尝试并搜索了所有可以想到的内容,但是找不到答案,我有一个简单的数据库

技术人员

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

维护

+------------------+------------+------+---------+
| 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   |         |
+------------------+------------+------+---------+

我所需要显示的是每个technicien的姓名,ID和薪水,每个maintenance的工作按维护的总数进行排序。

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

您需要针对表 technicien 的列进行 GROUP 分组,并根据 maintenance的计数对 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