leeman24
leeman24

Reputation: 2899

SQL Query using MySQL sample employee database

I have a problem making an advanced (for me) query.

I am trying to get the first name, last name, title, department name, and first and last name of the manager. I am using this SQL in PHP if for some reason you needed to know. I limit to 10 because the database is enormous. I need only the entry for the employees latest data. They have more than one entry because of job promotions, etc.

I included everything I think you would need to understand my problem. Thanks if you can help.

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)

The database is the employees database from 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)

Upvotes: 0

Views: 4671

Answers (1)

Stock Overflaw
Stock Overflaw

Reputation: 3321

I am trying to get the first name, last name, title, department name, and first and last name of the manager

Using the theory (but it's probably not optimized if you have large tables), do it step by step. Assuming you have a $date PHP variable that contains a date (like the result from date('Y-m-d 00:00:00')), or otherwise you may use MySQL's date.

1/ get employees' identity:

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

2/ add his/her current title

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/ get his/her current department (from dept_emp) and retrieve the department's name

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/ get the department's manager (from dept_manager) and retrieve his/her name (from 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

There you can append a LIMIT (and an ORDER BY probably, you don't know which results will be picked in case of a limit), and/or add in the WHERE an emp_no for the employee(s) you want to have info about. Not tested though, but it's mainly to let you understand how to build the request.

This is theory, and this request is probably be as heavy to process as the one you wrote. Since you use SQL with PHP, it may be better to retrieve only the primary keys you're interested in, then send short requests to retrieve the information you want. Something like that I mean (using fake functions but the spirit's here):

$dateString="from_date<='$date' AND to_date>='$date'";
$qe=query("SELECT emp_no, first_name, last_name, dept_no FROM employees, dept_emp WHERE dept_emp.emp_no=employees.emp_no LIMIT 10 ORDER BY emp_no DESC");
while($r=fetch($qe)) {
  $qt=fetch(query("SELECT title FROM titles WHERE emp_no=".$qe['emp_no']." AND ".$dateString));
  $qd=fetch(query("SELECT dept_name FROM departments WHERE dept_no=".$qe['dept_no']." AND ".$dateString));
  $qm=fetch(query("SELECT first_name, last_name FROM employees AS e, dept_manager AS dm WHERE dept_no=".$qe['dept_no']." AND e.emp_no=dm.emp_no AND ".$dateString));
  //echo/process here
}

Hope this helps :)

Upvotes: 1

Related Questions