MySQL 函数返回要在查询中使用的表名称

发布于 2024-08-03 03:32:40 字数 692 浏览 4 评论 0原文

我可以使用 MySQL 函数或过程返回可在查询中使用的表名称吗?

类似

SELECT * FROM get_db_table_name(2342352412);

其中 get_db_table_name() 是一个用户定义的函数,它接受 user_id 作为参数并返回该用户所在的 db.table。我无法以这种方式执行此操作,因为 MySQL 抱怨语法(我的猜测是因为我必须为该函数定义一个返回类型,我尝试将其作为 VARCHAR(30) ,并且它是错误的或不可能的)。那么,这可能吗?

故事是我有一些本质上是分片的数据库。我们将它们称为 user_1user_2 等。

我还有一个表,例如 emails,其中包含带有 user_id 的记录>s 来自表 user_1.usersuser_2.users 等。

因为我知道一种仅通过查看 id 即可计算 id 来自哪个表的方法,所以我只是想要一个接受输入并返回要使用的 db.table 名称的函数。

谢谢你!

PS 我想使用上面的 SELECT 查询作为 VIEW 定义,通过在正确的表中执行 JOIN 来从正确的数据库/表中获取用户名。

Can I use a MySQL function or procedure to return table names that can be used in a query?

Something like

SELECT * FROM get_db_table_name(2342352412);

where get_db_table_name() is a user defined function which accepts a user_id as a parameter and returns the db.table where that user resides. I have not been able to do this this way because MySQL complains about the syntax (my guess is it's because I had to define a return type for the function, which I tried as VARCHAR(30) and it is wrong or impossible). So, is this possible?

The story is that I have a few databases that are essentially shards. Let's call them user_1, user_2, etc.

I also have a single table, say emails that contains records with user_ids from tables user_1.users, user_2.users, etc.

Since I know a way to compute which table an id is from just by looking at the id, I just want a function that would accept an input and return a db.table name to be used.

Thank you!

P.S. I'd like to use the above SELECT query as a VIEW definition that would bring the user's name from the proper db/table by doing a JOIN across the proper table.

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

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

发布评论

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

评论(3

绅刃 2024-08-10 03:32:40

您可以使用准备好的语句这个,例如:

....
SET @tbl = 'nameofmytable';
SET @sql = CONCAT( 'SELECT * FROM ', @tbl );
EXECUTE @sql;
...

You can use prepared statements for this, e.g.:

....
SET @tbl = 'nameofmytable';
SET @sql = CONCAT( 'SELECT * FROM ', @tbl );
EXECUTE @sql;
...
离线来电— 2024-08-10 03:32:40

定义视图时您必须指定表。您无法在“从需要的地方选择”上定义视图...

视图定义在创建时“冻结”,因此之后对基础表的更改不会影响视图定义。例如,如果视图在表上定义为 SELECT *,则稍后添加到表中的新列不会成为视图的一部分。

您必须指定我对此、此和此表的看法。看看这个,您不能执行 SELECT * FROM func(id) 因为您无法指定该函数的结果是什么(取决于输入参数)。

选择的语法是

从表引用中选择*

“动态”SELECT 的唯一方法是使用动态 SQL,@Zed 的回答...

如果我的答案不完整,我很抱歉,但我不知道你的问题的详细信息。祝你好运!

When defining a view you must specify the tables. You can not define a view on "SELECT FROM whereever needed" ...

The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition. For example, if a view is defined as SELECT * on a table, new columns added to the table later do not become part of the view.

You must specify my view is on this, this and this table. Looking at this you can not do SELECT * FROM func(id) because you can not specify what the results of this function will be (depending on the input parameter).

The syntax of SELECT is

SELECT * FROM table_references

The only way of "dynamic" SELECT is with dynamic SQL, what @Zed answered...

I'm sorry if my answer is incomplete, but I don't know your problem in details. Good Luck!

终遇你 2024-08-10 03:32:40

参数化表名通常被认为是由于糟糕的数据库设计。难道真的不可能将同一类型的所有实体(在本例中为您的用户)放在一张表中吗?

Parameterising the table name is considered to be usually because of bad database design. Is it really not possible to have all the entities of the same type (in this case, your users) in one table?

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