2013-10-23 122 views
0

我在Mysql中使用Employee數據庫。我的數據庫包含以下表MySQL 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 |  | 
+-------+-------------+------+-----+---------+-------+ 

我得值部門,DESIG,基本,PF分別從表中edept.dept,edesig.desig,esal.basic,esal.pf。
我用所有的字段,我必須從其他tables.And檢索值的外鍵。我試着內部聯接查詢示例。但我得到的輸出如下所示:

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 

回答

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 
+0

的MySQL> SELECT * FROM edetails由部門內部聯接edept上edetails.dept = edept.id基引發錯誤ERROR 1052 (23000):集團聲明中的「部門」列不明確 – Swetha

+0

@Swetha:已更新我的回答,請檢查 –

+0

否Shashi我仍然得到相同的輸出 – Swetha