0

I'm new to Sql and can't seem to find an answer anywhere!...

I have a table "a" that contains a column "table_name" with the name of another table "b".

I would like to query the columns in table "a" and add an extra column with a count of the rows in table "b".

Something along these lines:

SELECT *, (SELECT COUNT(*) FROM a.table_name) AS num_rows FROM a;

Unfortunately, I can't reference a table in the "FROM" clause like this. How can I go about this?

Thanks in advance!

UPDATE

Just to clarify, I want all the rows in table "a" with an added column containing the row count of table "b": the name of table "b" is in column "table_name" in table "a".

GoldenLab88
  • 184
  • 2
  • 10
  • Put the table name in a variable, then visit the [original answer](http://stackoverflow.com/questions/8809943/how-to-select-from-mysql-where-table-name-is-variable) – Déjà vu Nov 24 '16 at 15:14
  • put tricky dynamic strings together with a `concat` noting that you can have more than 2 parameters to concat such as `concat( ... , .... , .... )` and see http://stackoverflow.com/a/40710949 – Drew Nov 24 '16 at 15:25
  • @ringø I've already tried this but maybe I'm not seeing the solution. I want *all* the rows in table "a" with an added column containing the row count of table "b": the name of table "b" is in column "table_name" in table "a". – GoldenLab88 Nov 25 '16 at 08:27

0 Answers0