sql - MySQL Inner Join table based on column value -


Suppose I have 'statistics' table with the following structure: tableName | ID | Pageviews The TableName column corresponds to different tables in the database.

When a query runs against "statistics", what would be the best way to get inside the internal column to get the data of each column in the table table? I was thinking of selecting dynamic in a foreach and then merging the results. Example: $ <$> $ ($ tableNames $ tableName) {$ sql = "SELECT * FROM with data INNER JOIN $ tableName tbl ON s.id = tbl.id WHERE Table name = '$ tableName'"; }

To get statistics of all the tables, you can use a union , 2 or more selections, one for each table:

  (SELECT s. *, Table1.title AS name - or whatever field you want to show with statistics, Add $ table name 1 table 1 s.id = Table1.id where table name = '$ tableName1') Unius all (SELECT s. *, Table2.name AS Names - or whatever fields you want to show with the statistics $ tablename 2 name2 table2s Id = table2.id WHERE table name = '$ TableName2') = UNION ALL (SELECT s. *, Table3.lastname AS Names - or whatever fields you want to show with statistics $ table name 3 table 3 s.id = table3.id where the filename = '$ tableName3' );   

With the idea of ​​Winfred LEFT JOIN S produces different results, e.g. Each field from other tables has output in its column (and many tap).

  SELECT s *, Table1.title - or whatever field you want to show, table2.name, table3.lastname --etc from the left side of the data $ tablename 1 table 1 s.id = table1.id and s.tableName = '$ tableName1 'Left join $ tablename 2 table 2 s.id = table2.id and s.tableName =' $ TableName2 'LEFT JOIN $ tableName3 table 3. s.id = table3.id and s.tableName =' $ tableName3 '- this ensures Omeatized table figures are not visible WHERE s.tablename IN ('$ tableName1', '$ TableName2', '$ tableName3');    

Comments