SQL:我们如何在 table1 的字段中给定的表上创建 table1 JOIN table2 ?

发布于 2024-10-02 06:49:10 字数 490 浏览 5 评论 0原文

假设我有 table1,其中有一列名为“table_name”。我使用 table1.table_name 来存储数据库中另一个表的名称。可引用的表都有一个字段“target_id”。

可以在 JOIN 语句中使用 table_name 吗?

例如:

SELECT t1.*, t2.* FROM table1 AS t1
                  JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id

显而易见的解决方案是使用脚本(在我的例子中是 C++)首先获取表名,然后从中构造一个 SQL 查询。问题是:我们可以绕过脚本并直接在 SQL (MySQL) 中执行此操作吗?

编辑:什么是动态 SQL?

Imagine I have table1 which has a column named 'table_name'. I use table1.table_name to store the name of another table in the database. The referenceable tables would all have a field 'target_id.

Is is possible to use table_name in a JOIN statement?

For example:

SELECT t1.*, t2.* FROM table1 AS t1
                  JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id

The obvious solution is to use the script (C++ in my case) to get the table name first, and construct a SQL query from it. The question is: can we bypass the script and do this directly in SQL (MySQL)?

Edit: What is dynamic SQL?

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

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

发布评论

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

评论(2

撑一把青伞 2024-10-09 06:49:10

你唯一的机会是执行 2 个 SQL 语句:

  • 选择你需要的表名,
  • 使用这个表名动态构建第二个查询来获取你需要的数据 - 你想要的不可能直接用 SQL 来完成(而且它听起来你在某种程度上设计了错误的数据库 - 但如果不知道它的目标是什么,就很难说)。

The only chance you have is to do 2 SQL statements:

  • select the tablename you need
  • use this table-name to dynamically build the secound query to get the data you need - what you want isn't possible to do with SQL directly (and it sounds like you've designed your database wrong in some way - but that's hard to say without knowing what's the goal of it).
聊慰 2024-10-09 06:49:10

我知道我参加聚会迟到了,但我想提供不同的解决方案。我在审计表中经常看到这种事情。 table_name 列指的是“更改了什么表”,table1_id 指的是该表中发生更改的行的 ID。在这种情况下,审计表指向许多通常不会连接的不同表。

当然

SELECT t1.*, t2.*, t3.*, t4.*, t5.*

FROM table1 AS t1

left JOIN table2 AS t2 
    ON t1.table1_id = t2.target_id
    and t1.table_name = 'table2'

left JOIN table3 AS t3
    ON t1.table1_id = t3.target_id
    and t1.table_name = 'table3'

left JOIN table4 AS t4 
    ON t1.table1_id = t4.target_id
    and t1.table_name = 'table4'

left JOIN table5 AS t5 
    ON t1.table1_id = t5.target_id
    and t1.table_name = 'table5'

,主要缺点是每个可能引用的表都需要显式包含在 SQL 命令中。

您可以使用它作为您的选择列表来获得更优雅的输出:

SELECT 
     t1.*, 
     coalesce(t2.fieldA, t3.fieldA, t4.fieldA, t5.fieldA) as fieldA,
     coalesce(t2.fieldB, t3.fieldB, t4.fieldB, t5.fieldB) as fieldB

等等

I know I'm late to the party, but I wanted to offer a different solution. I see this sort of thing a lot in audit tables. The column table_name would refer to "what table was changed" and table1_id would refer to the ID of the row that changed in that table. In this case, the audit table is pointing back to many different tables that don't normally get joined.

Here goes:

SELECT t1.*, t2.*, t3.*, t4.*, t5.*

FROM table1 AS t1

left JOIN table2 AS t2 
    ON t1.table1_id = t2.target_id
    and t1.table_name = 'table2'

left JOIN table3 AS t3
    ON t1.table1_id = t3.target_id
    and t1.table_name = 'table3'

left JOIN table4 AS t4 
    ON t1.table1_id = t4.target_id
    and t1.table_name = 'table4'

left JOIN table5 AS t5 
    ON t1.table1_id = t5.target_id
    and t1.table_name = 'table5'

Of course, the main drawback is that each table that can be possibly referenced needs to be explicitly included in the SQL command.

You can get more elegant output using this as your select list:

SELECT 
     t1.*, 
     coalesce(t2.fieldA, t3.fieldA, t4.fieldA, t5.fieldA) as fieldA,
     coalesce(t2.fieldB, t3.fieldB, t4.fieldB, t5.fieldB) as fieldB

etc

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