Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL: Retrieve different columns from different tables based on third table column values

let’s say I have a table called table1 that has two columns: tableNames and columnNames and looks like this:

tableNames— columnNames
table_242432 colX342X234
table_134 colX12X3
….

So each row represents the name of another table in my database and the name of a specific column within this table. Even though the names are all different, the information within these columns in these different tables is of the same nature and I want to retrieve it as a single column.
I don’t know the number of rows in table1 beforehand.
Is there an easy way to do this in mysql or should I use separated queries and then stitch the results together using php?

I tried creating some queries but to no good result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No, there is no easy way.

In SQL, the table names and column names (and all other identifiers and expressions) are fixed at the time the query is parsed — i.e. before any rows are read. So there is no way a query can "discover" which tables and columns to read after it begins reading data.

The hard way is to make outer joins to ALL your tables, and use conditional expressions to identify the column based on the data values. In the following example, the quoted strings must be hard-coded in your expression, and the identifiers (without quotes) will return the value of the corresponding column.

SELECT
  CASE table1.tableNames 
  WHEN 'table_242432' THEN
    CASE table1.columnNames
    WHEN 'colX342X234' THEN table_242432.colX342X234
    END
  WHEN 'table_134' THEN
    CASE table1.columnNames
    WHEN 'colX12X3' THEN table_134.colX12X3
    END
  END AS `value`
FROM table1
LEFT OUTER JOIN table_242432 ON ...
LEFT OUTER JOIN table_134 ON ...

I’m supposing there is some join condition between table1 and each of the related tables, but you haven’t described it.

This still requires you to know every possible table and column up front, before you can write that query. If you have more than a few possibilities, the expression will be ridiculously large.

Also you have a Cartesian product between each of the subordinate tables, so the number of rows returned are multiplied together in dramatic ways.

This is a case where it’s really more appropriate to implement as multiple queries.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading