0

包含两个 SELECTS 的 UNION 的查询,其中包含 LOCATION_ID、STREET_ADDRESS、CITY、COUNTRY_NAME 和“部门编号”列。结果必须是所有位置的列表,其中包含该位置的部门数。该列表必须按部门数量从多到少的顺序排列。

SELECT locations.location_id, 
       locations.street_address, 
       locations.city, 
       locations.country_id 
FROM locations,departments
WHERE (locations.location_id = departments.location_id)
GROUP BY locations.location_id, 
         locations.street_address, 
         locations.city, 
         locations.country_id
UNION ALL
SELECT Count(departments.department_name) 
FROM departments
WHERE (locations.location_id = departments.location_id)
GROUP BY departments.department_id, departments.location_id
ORDER BY (departments.department_name) DESC;`
4

1 回答 1

1

为什么这不起作用?

  SELECT count() as c, 
         departments.department_name, 
         locations.location_id, 
         locations.street_address, 
         locations.city, 
         locations.country_id 
  FROM locations
  join departments ON locations.location_id = departments.location_id
  GROUP BY departments.department_name, locations.location_id, locations.street_address, locations.city, locations.country_id
于 2013-11-13T19:07:22.560 回答