How do I structure an SQL statement to run across multiple flat unrelated tables and display the result with the result of the select and the name of the table where the result came from.
The scenario is such that I have several tables with the same column name in each. It is data that I have received from outside parties that I store as it is in different tables.
Same tables look like:
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 I need an SQL statement that searches for student_name across all tables In pseudo code: 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
Give the result in the following presentation:
john doe, Table 1, pid john doe, Table 9, pid To make it a bit complicated, the column student_name might not be in all tables so the query needs to proceed graciously if doesn't find the column there.