2012-09-19 38 views
1

我有一個問題,我的朋友說我必須用表的映射......但我不知道什麼是表的映射,我不知道,我現在要做的,是正確的或不表映射的MySQL

我有這個表:員工

+-------+------------+------+-------------------------+ 
| ac_no | department | rank | email     | 
+-------+------------+------+-------------------------+ 
| 12ac | 01  | 08 | [email protected]    | 
| 1an4 | 02  | 08 | [email protected]    | 
| dr17 | 01  | 08 | [email protected]    | 
| 13IN | 01  | 05 | [email protected]   | 
| TE12 | 02  | 05 | [email protected]   | 
| GR45 | 01  | 05 | [email protected]   | 
+-------+------------+------+-------------------------+ 

從數據上表員工我必須插入到表的映射

我們的目標是讓所有有等級08的數據

+-------+------------+------+-------------------------+ 
| 12ac | 01  | 08 | [email protected]    | 
| 1an4 | 02  | 08 | [email protected]    | 
| dr17 | 01  | 08 | [email protected]    | 
+-------+------------+------+-------------------------+ 

然後查找排名爲05並且在同一個部門中的數據,然後獲取電子郵件......如果數據超過一個電子郵件將拆分;

+-------+---------------+------------+------------------------------+ 
| ac_no |email   | department |    email   | 
+-------+---------------+------------+------------------------------+ 
| 12ac | [email protected] | 01  | [email protected];[email protected] | 
| 1an4 | [email protected] | 02  | [email protected]     | 
| dr17 | [email protected] | 01  | [email protected];[email protected] | 
+-------+---------------+------------+------------------------------+ 

我想使用過程從表員工數據處理成表映射,但我不知道怎麼去說。我在工作臺上創建一個過程並運行它,以將數據插入到表映射中。

select ac_no,department,rank,email,(select department,email from employee where rank=05)head from employee where rank=08; 

但是,如果我運行此查詢結果subquery returns more than 1 row

很抱歉,如果我問的基本問題,但我真的不知道如何做到這一點。

+0

不知道你問什麼,但我想你想通過量h表使用外鍵和innoDB。應該看一個規範化的數據庫 – wesside

+1

@bigman如果它是一個小型數據庫,那麼確定,但是當你有一個非常大的數據庫時,反規範化將大大提高性能。無論如何,您都可以通過連接來獲取所需的數據。插入前檢查一切。 –

回答

0

嘗試這樣

select ac_no,department,rank,email from employee where rank = 08 and where department = all(select department from employee group by department) 
0
(select ac_no,department,rank,email from employee where rank=8) UNION (select ac_no,department,rank,email from employee where (rank,department) in (select rank,department from employee where rank=5 group by department having count(*)>1)) 
所有員工的

返回數據:與秩8不論部門

2的

1))與等級5具有用於至少一個多僱員等級5屬於同一個部門

+0

我嘗試這個,但我得到所有的排名.... 08,05,06 – Belajar

+0

@Belajar嗯...我試着自己運行它。它工作得很好。沒有返回具有不匹配的等級的數據 – user1675644