MariaDB:如果存在同名的持久性 table,我如何 select 临时 table?

MariaDB: How can I select a temporary table if there is a persistent table with the same name?

我创建了一个持久的tabletemp和一个临时的tabletemp,也就是两者同名。如何将 select/update/insert 专门用于持久性或临时性 table?我如何区分它们?

MariaDB Tutorial 说:

Note − Temporary tables are permitted to have the same name as an existing non-temporary table because MariaDB views it as a difference reference.

所以,我想应该可以参考这些 table 之一。这个问题与我在 SO 中提出的 question 有关,但退后一步。

如果临时 table 与现有非临时 table 同名,则临时 table 将隐藏非临时 table 的名称。

这意味着在 SQL 语句中您将无法引用非临时 table.

解决方法是,在创建临时 table 之前在非临时 table 上创建一个视图,因为该视图在内部保留对非临时 table 的引用:

CREATE TABLE t1 (a VARCHAR(100));
INSERT INTO t1 VALUES ("foo");
CREATE VIEW v_t1 AS SELECT a FROM t1;
CREATE TEMPORARY TABLE t1 (b VARCHAR(100));
INSERT INTO t1 VALUES ("bar");
SELECT * FROM v_t1;
SELECT * FROM t1;