2010-05-01 65 views
1

我需要幫助在4個表上構建VIEW。該視圖應包含以下列:創建Mysql視圖以從不同表中選擇顏色

ER.ID,ER.EMPID,ER.CUSTID,ER.STATUS,ER.DATEREPORTED,ER.REPORT,EB.NAME,CR.CUSTNAME,CR.LOCID,CL。 LOCNAME,DI.DEPTNAME

的別名是:

EMP_REPORT ER,EMP_BIO EB,CUST_RECORD CR,CUST_LOC CL,DEPT_ID DI

的數據模型:

describe EMP_REPORT; 
+--------------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+--------------+-------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| empid  | int(11)  | NO |  | NULL |    | 
| custid  | int(11)  | NO |  | NULL |    | 
| status  | varchar(32) | NO |  | NULL |    | 
| datereported | bigint(20) | NO |  | NULL |    | 
| report  | text  | YES |  | NULL |    | 
+--------------+-------------+------+-----+---------+----------------+ 

describe EMP_BIO; 
+--------+-------------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+--------+-------------+------+-----+---------+-------+ 
| empid | int(11)  | NO | PRI | NULL |  | 
| name | varchar(56) | NO |  | NULL |  | 
| sex | char(1)  | NO |  | NULL |  | 
| deptid | int(11)  | NO |  | NULL |  | 
| email | varchar(32) | NO |  | NULL |  | 
| mobile | bigint(20) | YES |  | NULL |  | 
| gtlk | varchar(32) | YES |  | NULL |  | 
| skype | varchar(32) | YES |  | NULL |  | 
| cvid | int(11)  | YES |  | NULL |  | 
+--------+-------------+------+-----+---------+-------+ 

describe CUST_RECORD; 
+----------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+----------+--------------+------+-----+---------+----------------+ 
| custid | int(11)  | NO | PRI | NULL | auto_increment | 
| custname | varchar(32) | NO |  | NULL |    | 
| address | varchar(255) | YES |  | NULL |    | 
| contactp | varchar(32) | YES |  | NULL |    | 
| mobile | bigint(20) | YES |  | NULL |    | 
| locid | int(11)  | NO |  | NULL |    | 
| remarks | text   | YES |  | NULL |    | 
| date  | int(11)  | YES |  | NULL |    | 
| addedby | int(11)  | YES |  | NULL |    | 
+----------+--------------+------+-----+---------+----------------+ 

describe CUST_LOC; 
+---------+-------------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+---------+-------------+------+-----+---------+-------+ 
| locid | int(11)  | NO | PRI | 0  |  | 
| locname | varchar(32) | NO |  | NULL |  | 
+---------+-------------+------+-----+---------+-------+ 

describe DEPT_ID; 
+----------+-------------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+----------+-------------+------+-----+---------+-------+ 
| deptid | int(11)  | NO |  | NULL |  | 
| deptname | varchar(32) | YES |  | NULL |  | 
+----------+-------------+------+-----+---------+-------+ 

表EMP_REPORT包含員工提交的報告,需要提取其中的所有顏色。該表中的empid應該用於在EMP_BIO(員工biodata)表中獲取相應的名稱。應使用EMP_REPORT中的custid來獲取CUST_RECORD(客戶記錄)中用於在CUST_LOC(客戶位置)表中獲取locname的相應位置。 EMP_REPORT中的empid用於在EMP_BIO表中獲取相應的deptid,然後用它從DEPT_ID(department id)表中獲取相應的deptname。

我嘗試使用不同的選擇查詢的聯合來構建視圖,但沒有得到正確的結果。請幫幫我。

+0

這是一個更好的版本.. http://pastebin.com/Pkmzamag :) – blacktooth 2010-05-01 16:42:57

回答

0

不要使用工會,加入使用:

SELECT ER.ID, ER.EMPID, ER.CUSTID, ER.STATUS, ER.DATEREPORTED, ER.REPORT, EB.NAME, CR.CUSTNAME, CR.LOCID, CL.LOCNAME, DI.DEPTNAME 
FROM EMP_REPORT er 
JOIN EMP_BIO eb ON eb.empid = er.empid 
JOIN CUST_RECORD cr ON cr.custid = er.custid 
JOIN CUST_LOC cl ON cl.locid = cr.locid 
JOIN DEPT_ID di ON di.deptid = eb.deptid 
+0

它的工作。謝謝加載! :) 我認爲我應該徹底的加入JOIN概念。 – blacktooth 2010-05-01 18:37:09

0

創建視圖,然後使用類似下面的東西(這是不準確的代碼,你需要在正確的表名等,以替代)

SELECT ER.ID ER.EMPID ER.CUSTID ER.STATUS ER.DATEREPORTED ER.REPORT EB.NAME CR.CUSTNAME CR.LOCID CL.LOCNAME DI.DEPTNAME FROM ER JOIN EB on er.whatever = eb.whatever JOIN CR on cr.whatever = eb.whatever JOIN DL on cr.whatever = dl.whatever JOIN DI on dl.whatever = di.whatever

無論您是加入的領域是什麼。