I have an interesting situation about my MySQL db structure.
Let's assume I have three database tables: table_Main, table_sub_A, table_sub_B
"table_Main" fields:
- RowID
- Code (this field is used to find the "table_sub_" table
- Name
Example "table_Main" rows:
1,A,Test A
2,B,Test B
Now, I want to build an SQL query which will retrieve all rows from table_Main but also get count(*) from corresponding tables for each table_Main row.
SELECT *, (SELECT COUNT(*) FROM table_sub_XXXX) AS TotalRecords FROM table_Main
Problem starts here. XXXX should be the "Code" column value for each corresponding table_Main row. How can I generate a dynamic table name inside sub-select query?
Thanks for your help.