为 MySQL 中的临时表授予选择权限

发布于 2024-09-17 01:34:42 字数 728 浏览 2 评论 0原文

我有一个 MySQL 数据库,其用户只能访问几个表。该用户已被授予此数据库的CREATE TEMPORARY TABLES权限,通过观察查询日志,我可以看到他们创建了一个临时表,然后在尝试从中进行选择时失败。执行 GRANT SELECT ON TABLE 'db'.'tmp_tbl' TO 'user'@'localhost'; 不起作用,因为此时该表不存在。

此页面上的用户评论建议hack 来做到这一点,但我希望有更好的方法。


使用它的代码有点像这样:

CREATE TEMPORARY TABLE t1 SELECT id FROM real1 LIMIT 10;
CREATE TEMPORARY TABLE t2 SELECT id FROM real2 LIMIT 10;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t1 JOIN t2 JOIN real3 AS r ON t1.id = r.a AND t2.id = r.b;

在这种情况下,重点是数据(或至少其中一些)是实时的。
我需要前两个查询的结果和第三个查询中的联接值相同,因此使用子查询不起作用。

I have a MySQL DB with a user that has access only a few tables. This user has been granted CREATE TEMPORARY TABLES for this database and by watching the query logs, I can see them create a temporary table and then fail while trying to select from it. Doing GRANT SELECT ON TABLE 'db'.'tmp_tbl' TO 'user'@'localhost'; doesn't work as the table doesn't exist at that point.

The user comments on this page suggest a hack to do this but I would have expected there to be a nicer way.


The code that uses this is sorta like this:

CREATE TEMPORARY TABLE t1 SELECT id FROM real1 LIMIT 10;
CREATE TEMPORARY TABLE t2 SELECT id FROM real2 LIMIT 10;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t1 JOIN t2 JOIN real3 AS r ON t1.id = r.a AND t2.id = r.b;

In this case, the whole point is that the data (or at least some of it) is live.
I need the results from the first two queries and the values for the join in the third to be the same so using sub queries won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-09-24 01:34:51

似乎正在进行的工作允许授予临时表的不仅仅是“创建”权限,但似乎尚未完成:http://bugs.mysql.com/bug.php?id=27480

您链接到的手册页中提到的解决方法也适合您。您可以在 MySQL 中跨数据库连接,因此假设您执行了解决方法中的 tmp 表技巧,您可以这样做:

SELECT tmp.t1.a, tmp.t2.b, realdb.real3.c FROM tmp.t1
       ^^^       ^^^       ^^^^^^
JOIN tmp.t2 ON  tmp.t1.somefield=tmp.t2.otherfield
     ^^^        ^^^              ^^^
JOIN realdb.real3 ON tmp.t2.yetanotherifeld = realdb.real3.yetanotherotherfield
     ^^^^^^          ^^^                      ^^^^^^
etc...

只需确保指定数据库(如示例中的 ^^^ 所示)在查询中,MySQL 会愉快地加入。

There appears to be work in progress to allow granting more than just "create" permissions for temp tables, but doesn't appear to be done yet: http://bugs.mysql.com/bug.php?id=27480.

The workaround mentioned in the man page you linked to would work for you as well. You can join across databases in MySQL, so assuming you did the tmp table trick from the workaround, you could do:

SELECT tmp.t1.a, tmp.t2.b, realdb.real3.c FROM tmp.t1
       ^^^       ^^^       ^^^^^^
JOIN tmp.t2 ON  tmp.t1.somefield=tmp.t2.otherfield
     ^^^        ^^^              ^^^
JOIN realdb.real3 ON tmp.t2.yetanotherifeld = realdb.real3.yetanotherotherfield
     ^^^^^^          ^^^                      ^^^^^^
etc...

just make sure to specify the databases (as shown by the ^^^ in the sample) in the query and MySQL will happily join away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文