创建一个Mysql视图以从不同的表中选择SELECT列

时间:2010-05-01 16:34:08

标签: sql mysql

我需要帮助在4个表上构建VIEW。该视图应包含以下列:

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(employee biodata)表中获取相应的名称。 EMP_REPORT中的custid应该用于获取CUST_RECORD(客户记录)中的相应locid,该客户端记录用于获取CUST_LOC(客户位置)表中的locname。 EMP_REPORT中的empid用于获取EMP_BIO表中的相应deptid,然后用于从DEPT_ID(部门ID)表中获取相应的deptname。

我尝试使用不同选择查询的联合构建视图,但没有得到正确的结果。请帮帮我。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

创建您的视图,然后使用类似于以下内容的东西(这不是确切的代码,您需要替换正确的表名等)

选择ER.ID,ER.EMPID,ER.CUSTID,ER.STATUS,ER.DATEREPORTED,ER.REPORT,EB.NAME,CR.CUSTNAME,CR.LOCID,CL.LOCNAME,DI.DEPTNAME来自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

无论你加入的领域是什么。