【发布时间】:2014-04-23 21:19:54
【问题描述】:
如何构造一条 SQL 语句以在多个平面不相关的表中运行并显示结果以及选择的结果和结果来自的表的名称。
这种情况是这样的,我有几个表,每个表都有相同的列名。这是我从外部各方收到的数据,我将其存储在不同的表中。
相同的表格看起来像:
Table 1: pid, parent_name, student_name, student_number, class_name, columnN
Table 2: pid, previous_school, previous_school, student_number, columnN
Table 3: pid, student_name, student_number, parent_name, column4, columnN
Table 14: pid, student_number, parent_name, column4, columnN
Table N: pid, previous_school, parent_name, column4, columnN
我需要一个在所有表中搜索 student_name 的 SQL 语句
在伪代码中:for each table, find a student named john doe and return to me the row where you got the result and the table where you found the result
在下面的演示文稿中给出结果:
john doe, Table 1, pid
john doe, Table 9, pid
为了让它有点复杂,student_name 列可能不在所有表中,因此如果在那里没有找到该列,查询需要继续进行。
【问题讨论】:
-
您可以考虑为您的数据库设计继承。 Example with links. 在这种情况下,您将对this method to return the name of the actual source table. 感兴趣
标签: sql postgresql database-design dynamic-sql