我有一个MySQL数据库集,因此任务数据会自动发布到数据库中。每个任务都在一个部分中,每个部分都有自己的表。有20个表格中包含所有信息。我需要将所有这些信息都放到单个视图中,以便我可以直观地显示它。目前我正在为每项任务执行以下操作,并且有118项任务。
启动MySQL表
**Main_table**
ID | Dis | Div | inspect
-----|-----|-----|--------
1001 | as | ad | Yes
1111 | pq | ad | No
CREATE TABLE Main_table(
ID INTEGER NOT NULL PRIMARY KEY
,Dis VARCHAR(2) NOT NULL
,Div VARCHAR(2) NOT NULL
,inspect VARCHAR(3) NOT NULL
);
INSERT INTO mytable(ID,Dis,Div,inspect) VALUES (1001,'as','ad','Yes');
INSERT INTO mytable(ID,Dis,Div,inspect) VALUES (1111,'pq','ad','No');
**Sec10_table**
ID | task101
----|--------
1001| Null
1111| Good
CREATE TABLE Sec10_table(
ID INTEGER NOT NULL PRIMARY KEY
,task101 VARCHAR(4)
);
INSERT INTO mytable(ID,task101) VALUES (1001,NULL);
INSERT INTO mytable(ID,task101) VALUES (1111,'Good');
**Sec11_table**
ID | Task111 | Task112
-----|---------|--------
1001 | Bad | Good
1111 | Good | Bad
CREATE TABLE Sec11_table(
ID INTEGER NOT NULL PRIMARY KEY
,Task111 VARCHAR(4)
,Task112 VARCHAR(4)
);
INSERT INTO mytable(ID,Task111,Task112) VALUES (1001,'Bad','Good');
INSERT INTO mytable(ID,Task111,Task112) VALUES (1111,'Good','Bad');
我现在在做什么
Create View Master_view As
Select Main_table.ID AS ID,
'Task 101' AS Task,
Sec10_table.Task101 AS Results,
Main_table.Dis AS Dis,
Main_table.Div AS Div,
Main_table.inspect AS Inspect,
10 AS Section
From (Main_table
LEFT JOIN Sec10_table ON (Main_table.ID = Sec10_table.ID))
Where Sec10_table.Task101 IS NOT NULL
UNION ALL
Select Main_table.ID AS ID,
'Task 111' AS Task,
Sec11_table.Task111 AS Results,
Main_table.Dis AS Dis,
Main_table.Div AS Div,
Main_table.inspect AS Inspect,
11 AS Section
From (Main_table
LEFT JOIN Sec11_table ON (Main_table.ID = Sec11_table.ID))
WHERE Sec11_table.Task111 IS NOT NULL
UNION ALL
Select Main_table.ID AS ID,
'Task 112' AS Task,
Sec11_table.Task112 AS Results,
Main_table.Dis AS Dis,
Main_table.Div AS Div,
Main_table.inspect AS Inspect,
11 AS Section
From (Main_table
LEFT JOIN Sec11_table ON (Main_table.ID = Sec11_table.ID))
WHERE Sec11_table.Task112 IS NOT NULL
创建此视图。
**Master_View**
ID | Task | Results | Dis | Div | Inspect | Section
-----|----------|---------|------|------|---------|--------
1001 | Task 111 | Bad | as | ad | Yes | 11
1001 | Task 112 | Good | as | ad | Yes | 11
1111 | Task 101 | Good | pq | ad | No | 10
1111 | Task 111 | Good | pq | ad | No | 11
1111 | Task 112 | Bad | pq | ad | No | 11
我一直试图找到不同的方法,这可能会使它更简单,更有效,因为目前运行速度太慢。