我正在尝试在 MYSQL 中创建一个 View ,但是当使用 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;
最佳答案
从您的架构中可以收集到的信息来看,您似乎想要从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;
关于mysql - 使用 union all 在 mysql 中创建 View ,但所有记录都显示在 1 列下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28286955/