员工详细信息无法正常显示

时间:2013-03-21 09:05:51

标签: mysql sql select join

嗨,这是数据库结构。

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 个答案:

答案 0 :(得分:2)

要避免语句上出现语法错误,请使用反引号包含包含非文字字符(,如减号,空格,... )的列名。

First Query,

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

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

答案 3 :(得分:-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)

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