1

您好,这是数据库结构。

employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)

我需要获取以下详细信息,但根据我的培训师的说法,任何人都可以告诉我以下查询有什么问题吗?

查找在 First Bank Corporation 工作且收入超过 10,000 美元的所有员工的姓名、街道地址和居住城市。

SELECT e.employee-name, e.city
FROM employee e
INNER JOIN works w
ON e.employee-name = w.employee-name
AND w.company-name = “First Bank Corporation”
GROUP BY e.employee-name
HAVING w.salary > 10000

在数据库中查找与他们工作的公司居住在同一城市的所有员工。

SELECT e.employee-name
FROM employee e
INNER JOIN works w
ON e.employee-name = w.emploee-name
INNER JOIN company c
ON w.company-name = c.company-name
GROUP BY e.employee-name
HAVING e.city = c.city

在数据库中查找与其经理居住在同一城市和同一街道上的所有员工。

SELECT employee-name, street, city
FROM employee
INTERSECT
SELECT e.employee-name, e.street, e.city
FROM employee e, manages m
WHERE e.employee-name = m.manager-name
4

4 回答 4

2

为避免语句出现语法错误,请将包含非文字字符(如减号、空格...)的列名用反引号括起来。

第一次查询,

SELECT  e.`employee-name`, e.city
FROM    employee e
        INNER JOIN works w
            ON e.employee-name = w.employee-name
WHERE   w.`company-name` = 'First Bank Corporation' AND
        w.salary > 10000

第二,

SELECT  e.`employee-name`
FROM    employee e
        INNER JOIN works w
            ON e.`employee-name` = w.`employee-name`
        INNER JOIN company c
            ON w.`company-name` = c.`company-name`
WHERE   e.city = c.city

第三,

SELECT  emp.*
FROM    Manages a
        INNER JOIN employee man
            ON a.`manager-name` = man.`employee-name`
        INNER JOIN employee emp
            ON a.`employee-name` = emp.`employee-name`
WHERE   man.city = emp.city AND
        emp.street = man.street
于 2013-03-21T09:12:12.133 回答
1

你应该使用WHERE从句。

查找在 First Bank Corporation 工作且收入超过 10,000 美元的所有员工的姓名、街道地址和居住城市。

SELECT e.`employee-name`, e.street, e.city
FROM employee e
INNER JOIN works w ON e.`employee-name` = w.`employee-name`
WHERE w.company-name = "First Bank Corporation"
AND w.salary > 10000

使用 WHERE 子句而不是GROUP BYHAVING

在数据库中查找与他们工作的公司居住在同一城市的所有员工。

SELECT e.`employee-name`
FROM employee e
INNER JOIN works w ON e.`employee-name` = w.`employee-name`
INNER JOIN company c ON w.`company-name` = c.`company-name`
WHERE e.city = c.city

在数据库中查找与其经理居住在同一城市和同一街道上的所有员工。

SELECT m.`employee-name`
FROM manages m
INNER JOIN employee e ON e.`employee-name` = m.`employee-name`
INNER JOIN employee manager ON manager.`employee-name` = m.`manager-name`
WHERE e.street = manager.street
AND e.city = manager.city
于 2013-03-21T09:11:52.120 回答
0

您可以通过使用 WHERE 而不是 GROUP BY 和 HAVING 来解决前 2 个问题。第一:

SELECT e.employee-name, e.city
FROM employee e
INNER JOIN works w
ON e.employee-name = w.employee-name
WHERE w.company-name = “First Bank Corporation”
AND w.salary > 10000

第二个:

SELECT e.employee-name
FROM employee e
INNER JOIN works w
ON e.employee-name = w.employee-name
INNER JOIN company c
ON w.company-name = c.company-name
WHERE e.city = c.city
于 2013-03-21T09:16:00.400 回答
-1

这是我解决第一个问题的方法:

SELECT employee-name, street, city 
FROM employee 
WHERE employee-name = (
SELECT employee-name 
FROM works where company-name = "First Bank Corporation" 
AND salary > 10000)

如果您需要其他帮助,请告诉我

于 2013-03-21T09:12:01.400 回答