I wonder if its possible to make query like this one:

SELECT cs.platform,
     (SELECT COUNT(*) FROM general_list as loc WHERE loc.platform=cs.platform) as indexed,
     (SELECT COUNT(*) FROM cs.table     as loc WHERE loc.platform=cs.platform) as sandbox
    SELECT platform, table
    FROM states
    WHERE visible=1
) as cs

For each iteration in the main select, I need to take the table name from cs.table to info from such table.

Is there anyway to accomplish this?

  • Sorry, it was a bug! I have fixed it. Thanks. – Apalabrados Jan 18 '17 at 22:29
  • The field 'table' in states holds the table names. The issue is that in the second select (sandbox) I want to use the name of the table in the field (cs.table). – Apalabrados Jan 18 '17 at 22:36
  • Please, it is not clear, add your table schema. – McNets Jan 18 '17 at 22:38
  • I suppose you're trying to use the content of table field on a FROM sentence. You should have a look at dynamic queries. – McNets Jan 18 '17 at 22:42

The only way to "dynamically" use a tablename is via a Stored Routine in which you do CONCAT(...) to build the SQL, plus PREPARE and EXECUTE to then perform the constructed SQL.

Needing to do this is usually a sign of poor schema design. Please explain the schema and why you went that direction; we may be able to help 'fix' it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.