我试图在MYSQL中创建一个视图但是当使用Union ALL时,它显示在1列下面。当我尝试添加列名时,它只是说列数和结果不匹配
use TestDB;
DROP VIEW if exists auditTableView;
Create view auditTableView
AS
select usernames.username from usernames LEFT JOIN auditTable on usernames.ID = auditTable.userID
Union ALL
select actionDesc.functionName from actionDesc LEFT JOIN auditTable on actionDesc.actionID = auditTable.actionID
Union all
SELECT timestamp from auditTable;
SELECT * FROM auditTableView;
答案 0 :(得分:1)
从可以收集的模式中,您看起来想要从auditTable到您的用户名和actionDesc表进行内部联接,并从那里选择用户名,actionDesc和时间戳值:
use TestDB;
DROP VIEW if exists auditTableView;
Create view auditTableView
AS
select
usernames.username,
actionDesc.functionName,
auditTable.timestamp
from auditTable
inner join usernames
on usernames.ID = auditTable.userID
inner join actionDesc
on actionDesc.actionID = auditTable.actionID;
select * from auditTableView;
答案 1 :(得分:0)
使用UNION
,MySQL使用第一个SELECT
语句中的列名,列数必须匹配。
您可以这样做:
SELECT table1.col1, NULL AS col2 FROM table1
UNION ALL
SELECT NULL AS col1, table2.col2 FROM table2