MySQL数据库连接查询

时间:2013-10-23 08:57:39

标签: mysql

我正在使用Mysql中的Employee数据库。我的Db包含以下表格

    mysql> describe edept;     
+-------+-------------+------+-----+---------+-------+  
| Field | Type        | Null | Key | Default | Extra |  
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| dept  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+


mysql>describe esal;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
| basic | int(11) | NO   |     | NULL    |       |
| pf    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

mysql> describe edesig;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| desig | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+


mysql> select * from edetails inner join edept on edetails.dept=edept.id;
+----+--------+-----+------+-------+-------+----+----+------------------+
| id | name   | age | dept | desig | basic | pf | id | dept             |
+----+--------+-----+------+-------+-------+----+----+------------------+
|  1 | swetha |  21 |    3 |     2 |     2 |  2 |  3 | Business Process |
+----+--------+-----+------+-------+-------+----+----+------------------+


mysql> describe edetails;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | int(11)     | NO   |     | NULL    |       |
| dept  | int(11)     | NO   | MUL | NULL    |       |
| desig | int(11)     | YES  | MUL | NULL    |       |
| basic | int(11)     | NO   | MUL | NULL    |       |
| pf    | int(11)     | NO   | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+

我必须分别从表edept.dept,edesig.desig,esal.basic,esal.pf中获取 dept,desig,basic,pf 的值。
   我为所有字段使用外键,我必须从其他表中检索值。我尝试了一个示例内连接查询。但我得到的输出如下:

 mysql> select * from edetails inner join edept on edetails.dept=edept.id;
    +----+--------+-----+------+-------+-------+----+----+------------------+
    | id | name   | age | dept | desig | basic | pf | id | dept             |
    +----+--------+-----+------+-------+-------+----+----+------------------+
    |  1 | swetha |  21 |    3 |     2 |     2 |  2 |  3 | Business Process |
    +----+--------+-----+------+-------+-------+----+----+------------------+

My edept table contains the following:


 mysql> select * from edept;
+----+------------------+
| id | dept             |
+----+------------------+
|  3 | Business Process |
+----+------------------+



How can i eliminate duplicate columns. i need the value "business process" in the dept field of the edept table

1 个答案:

答案 0 :(得分:0)

试试这个::

    select 
    edetails.id,
    edetails.name,
    edetails.age,
    edetails.dept, 
    edesig.desig,
    edetails.basic,
    edetails.pf,
    edept.dept

     from edetails 
        inner join edept on edetails.dept=edept.id
INNER JOIN edesig on edesig.id=edetails.desig