2012-03-26 33 views
0

我有一個問題,使高級(對我)查詢。使用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

那麼,有什麼問題嗎?你是否收到錯誤信息?結果與您所期望的不同? – 2012-03-26 02:28:26

+0

此外,請參閱http://stackoverflow.com/editing-help瞭解降價的概述,並參閱「?」在編輯帖子時在工具欄中。也懸停在圖標上,看看他們做了什麼。有一個地方你可以選擇一段文字變成「代碼塊」(這是通過縮進四個空格來完成的)。它看起來像一對大括號{}'。 – 2012-03-26 02:29:15

+0

爲了便於閱讀,我編輯了您的帖子,接受它。就像Ben Lee說的那樣,你的問題是什麼 – grifos 2012-03-26 02:33:10

回答

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)和重新trieve他/她的名字(從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可能的是,你不知道哪個結果的情況下將採摘的限制),和/或添加WHEREemp_no爲您想要了解有關信息的員工。雖然未經過測試,但主要是讓您瞭解如何構建請求。

這是理論,這個請求可能和你寫的一樣重。由於您在PHP中使用SQL,因此最好只檢索您感興趣的主鍵,然後發送簡短的請求以檢索所需的信息。喜歡的東西,我的意思是(使用假功能,但精神在這裏):

$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 
} 

希望這有助於:)

+0

這確實有幫助。實際上有一點。 我確實想要做一個LIMIT和ORDER BY,因爲我會對它進行排序,例如,列出所有 - 使用50或者其他分頁限制,並且僅由一個或多個部門排序... 當我添加ORDER通過empFN LIMIT 10或類似的東西,MySQL失速。 順便說一下,我在MySQL網站上嘗試過這種方式。 – leeman24 2012-03-26 18:02:33

+0

你是什麼意思它攤位? MySQL崩潰?日誌中的任何內容(尤其是[緩慢查詢](http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html)機制:日誌和配置)?無論如何,如果你的目標是按部門排序(不知道你的「一個或多個」部分需要理解什麼),可以使用'ORDER BY'和'dept_no':在任何情況下避免使用'索引' ORDER BY'(和'employees.first_name'可能沒有被索引),特別是當它應用的請求返回巨大的結果時,因爲它會使它更「慢」(我的意思是**非常慢)。 – 2012-03-26 23:36:57

+0

無論如何你可以做什麼,因爲它是一個非常繁重的請求處理,是使用[一個視圖](http://dev.mysql.com/doc/refman/5.0/en/create-view.html)或[臨時表](http://dev.mysql.com/doc/refman/5.1/en/create-table.html),以避免每次頁面重新加載時發送這個大的錯誤請求。這樣,來自PHP頁面的請求將非常快速(基本上類似於'SELECT * FROM your_view where 1 ORDER BY some_field LIMIT $ offset,10')。查看優點:在服務器上一勞永逸地創建;缺點:沒有索引。 TempTable優點:可以定義索引;缺點:僅在連接期間創建。 – 2012-03-26 23:59:18