2014-11-23 44 views
0

我有2个表,内容如下寻找失踪雇员薪金表

`mysql> describe 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.01 sec) 

mysql> describe 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.00 sec)` 

如果考虑到每一个员工必须受薪我预计行 的数量在工资表是相同的员工因此员工 中的所有emp_no都在薪水中。但是在执行Count语句时,我会看到下面的结果。

`mysql> select count(*) from employees; 
+----------+ 
| count(*) | 
+----------+ 
| 300024 | 
+----------+ 
1 row in set (0.00 sec) 
mysql> select count(*) from salaries; 
+----------+ 
| count(*) | 
+----------+ 
| 2844047 | 
+----------+ 
1 row in set (0.00 sec) 

这表明,有少数人存在于员工,但薪水缺,所以我做了 下面查询发现,如果有任何。

`mysql> select * from employees where emp_no not in (select emp_no from salaries) ; 
Empty set (0.96 sec)` 

EMP_NO是主键是唯一的,因此为什么会出现在两个表中不同的行数,即使 上述结果返回空集时,我使用不查询它。

我在雇员中也发现了一些相同的名字,但雇佣日期不同,但emp_no 对他们来说是不同的。

`mysql> select * from employees where first_name='Zvonko' and last_name='Perko'; 
+--------+------------+------------+-----------+--------+------------+ 
| emp_no | birth_date | first_name | last_name | gender | hire_date | 
+--------+------------+------------+-----------+--------+------------+ 
| 20909 | 1961-04-24 | Zvonko  | Perko  | F  | 1990-12-14 | 
| 488633 | 1961-08-01 | Zvonko  | Perko  | F  | 1995-08-08 | 
+--------+------------+------------+-----------+--------+------------+ 

我哪里错了?有人能让我知道吗?

+0

难道你不觉得“薪水”表中的记录数量惊人吗? ) – raina77ow 2014-11-23 17:32:30

+0

2844047> 300024 – fthiella 2014-11-23 17:43:29

+0

我猜想我的错误,因为我误读了薪水中的行数。我想我想关闭这个问题。 – Invictus 2014-11-23 17:48:12

回答

1

您的薪水表比您的员工表有更多的行。大约三百万对大约三十万。

工资表有两部分主键,这意味着一个员工可能有多个不同的from_dates工资。你知道,加薪和东西。

+0

这真的很难在评论中阅读。把它放在你的问题中,而不是相关。 – 2014-11-23 17:38:38

+0

在问题编辑 – Invictus 2014-11-23 17:43:45

+0

我猜我的错误,因为我误读了薪水中的行数。我想我想关闭这个问题。 – Invictus 2014-11-23 17:47:37