MySQL创建视图,它是两个表的并集,前面有一个表值

时间:2013-01-10 14:46:58

标签: mysql sql database union temp-tables

我有从MySQL数据库获取权限的身份验证系统。

CREATE TEMPORARY TABLE db.temp (authority VARCHAR(100));
INSERT INTO db.temp (authority) SELECT blog.authors.id FROM db.authors;
UPDATE db.temp SET authority=CONCAT("AUTH_",authority);

SELECT authority FROM db.temp
UNION
SELECT authority FROM db.authorities_real

从命令行执行此脚本时工作正常,但在Views中,不允许使用临时表。有没有办法在没有它们的情况下做我想要的事情?

2 个答案:

答案 0 :(得分:2)

为什么不只是UNION使用authors表:

SELECT CONCAT('AUTH_',blog.authors.id)
FROM db.authors
UNION 
SELECT authority 
FROM db.authorities_real

您只需使用db.authors中的UNION表,而不是创建临时表。这样做你不必使用临时表。

UNION将从查询中删除任何重复项。如果您不介意重复或不期望任何重复,那么我会使用UNION ALL

答案 1 :(得分:0)

您可以将这些语句合并为一个语句:

SELECT concat('AUTH_', blog.authors.id) as authority FROM db.authors
UNION -- all ?
SELECT authority FROM db.authorities_real`