SQL Query使用MySQL示例员工数据库

时间:2012-03-26 02:24:55

标签: php mysql sql

我在制作高级(对我而言)查询时遇到问题。

我正在尝试获取名字,姓氏,职位,部门名称以及经理的名字和姓氏。如果由于某种原因你需要知道我在PHP中使用这个SQL。我限制为10,因为数据库是巨大的。我只需要为员工输入最新数据。由于工作促销等原因,他们有多个条目。

我包含了我认为您需要了解我的问题所需的一切。谢谢,如果你能提供帮助。

mysql>
SELECT DISTINCT employees.first_name, employees.last_name,
titles.title, departments.dept_name, z.first, z.last
FROM employees, dept_emp, departments, titles, 
(
  SELECT employees.first_name AS first, employees.last_name AS last
  FROM employees, dept_emp, dept_manager 
  WHERE 
        employees.emp_no = dept_emp.emp_no 
        AND dept_manager.emp_no = dept_emp.emp_no
) AS z
WHERE
   employees.emp_no = dept_emp.emp_no 
   AND dept_emp.dept_no = departments.dept_no
   AND titles.emp_no = employees.emp_no
LIMIT 10;

+------------+-----------+-----------------+-------------+-----------+--------------+
| first_name | last_name | title           | dept_name   | first     | last         |
+------------+-----------+-----------------+-------------+-----------+--------------+
| Georgi     | Facello   | Senior Engineer | Development | Margareta | Markovitch   |
| Georgi     | Facello   | Senior Engineer | Development | Vishwani  | Minakawa     |
| Georgi     | Facello   | Senior Engineer | Development | Ebru      | Alpin        |
| Georgi     | Facello   | Senior Engineer | Development | Isamu     | Legleitner   |
| Georgi     | Facello   | Senior Engineer | Development | Shirish   | Ossenbruggen |
| Georgi     | Facello   | Senior Engineer | Development | Karsten   | Sigstam      |
| Georgi     | Facello   | Senior Engineer | Development | Krassimir | Wegerle      |
| Georgi     | Facello   | Senior Engineer | Development | Rosine    | Cools        |
| Georgi     | Facello   | Senior Engineer | Development | Shem      | Kieras       |
| Georgi     | Facello   | Senior Engineer | Development | Oscar     | Ghazalie     |
+------------+-----------+-----------------+-------------+-----------+--------------+
10 rows in set (0.00 sec)

数据库是mysql.com的员工数据库: http://dev.mysql.com/doc/index-other.html

mysql> desc departments;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no   | char(4)     | NO   | PRI | NULL    |       |
| dept_name | varchar(40) | NO   | UNI | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.22 sec)

mysql> desc dept_emp;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| dept_no   | char(4) | NO   | PRI | NULL    |       |
| from_date | date    | NO   |     | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.20 sec)

mysql> desc dept_manager;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| dept_no   | char(4) | NO   | PRI | NULL    |       |
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| from_date | date    | NO   |     | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.21 sec)

mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no     | int(11)       | NO   | PRI | NULL    |       |
| birth_date | date          | NO   |     | NULL    |       |
| first_name | varchar(14)   | NO   |     | NULL    |       |
| last_name  | varchar(16)   | NO   |     | NULL    |       |
| gender     | enum('M','F') | NO   |     | NULL    |       |
| hire_date  | date          | NO   |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.32 sec)

mysql> desc salaries;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| salary    | int(11) | NO   |     | NULL    |       |
| from_date | date    | NO   | PRI | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.34 sec)

mysql> desc titles;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| emp_no    | int(11)     | NO   | PRI | NULL    |       |
| title     | varchar(50) | NO   | PRI | NULL    |       |
| from_date | date        | NO   | PRI | NULL    |       |
| to_date   | date        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.60 sec)

1 个答案:

答案 0 :(得分:1)

  

我正在尝试获取名字,姓氏,职位,部门名称以及经理的名字和姓氏

使用理论(但如果你有大表,它可能没有优化),一步一步地做。假设你有一个包含日期的$date PHP变量(比如date('Y-m-d 00:00:00')的结果),或者你可以使用MySQL的日期。

1 /获得员工身份:

SELECT
    e.first_name,
    e.last_name
FROM
    employees AS e
WHERE
    1

2 /添加他/她当前的标题

SELECT
    e.first_name,
    e.last_name,
    t.title
FROM
    employees AS e,
    titles AS t
WHERE
    e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'

3 /获取他/她当前的部门(来自dept_emp)并检索部门的名称

SELECT
    e.first_name,
    e.last_name, t.title,
    d.dept_name
FROM
    employees AS e,
    titles AS t,
    dept_emp AS de,
    departments as d
WHERE
    e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
AND
    e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
AND
    d.dept_no=de.dept_no

4 /获取部门经理(来自dept_manager)并检索他/她的名字(来自employees

SELECT
    e.first_name AS empFN,
    e.last_name AS empLN,
    t.title AS empT,
    d.dept_name AS dept,
    em.first_name AS manFN,
    em.last_name AS manLN
FROM
    employees AS e,
    titles AS t,
    dept_emp AS de,
    departments as d,
    dept_manager AS dm,
    employees AS em
WHERE
    e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
AND
    e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
AND
    d.dept_no=de.dept_no
AND
    em.emp_no=dm.emp_no AND dm.from_date<='$date' AND dm.to_date>='$date'
AND
    de.dept_no=dm.dept_no

在那里你可以追加一个LIMIT(和一个ORDER BY,你可能不知道在有限制的情况下会选择哪些结果),和/或添加{{1}您希望获得相关信息的员工WHERE。虽然没有经过测试,但它主要是为了让您了解如何构建请求。

这是理论,这个请求可能和你写的一样重要。由于您将SQL与PHP一起使用,因此最好只检索您感兴趣的主键,然后发送短请求以检索所需的信息。我的意思是这样的(使用虚假功能但精神在这里):

emp_no

希望这会有所帮助:)