简体   繁体   English

是否有可能用另一个查询的结果替换查询中的 MySql 表名

[英]Is there any possibility to replace MySql table name in query with result of another query

How to use select result of one table as table name in subquery.如何在子查询中使用一张表的选择结果作为表名。 Is there any hack about that in MySql 5.6?在 MySql 5.6 中是否有任何破解?

No, the way SQL works is that all identifiers (eg table names, column names) and SQL syntax must be fixed at the time the query is prepared -- which is before it begins reading any data.不,SQL 的工作方式是所有标识符(例如表名、列名)和 SQL 语法必须在准备查询时固定——也就是在它开始读取任何数据之前。

Therefore no data the query reads during the execution phase can change the table or other fixed elements of the query.因此,查询在执行阶段读取的任何数据都不能更改查询的表或其他固定元素。

It would be like writing a function in your application where you wanted the return value of the function to determine which function to call.这就像在您的应用程序中编写一个函数,您希望函数的返回值来确定要调用哪个函数。

The workaround is that you have to do this in two steps.解决方法是您必须分两步执行此操作。 The first step is what you're expecting to run as a subquery.第一步是您期望作为子查询运行的内容。 It returns information you can use to choose which table to query in the main query.它返回可用于选择要在主查询中查询哪个表的信息。 But you have to fetch this from the first query before you prepare the second query.但是在准备第二个查询之前,您必须从第一个查询中获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM