SELECT * FROM (SELECT table_name FROM orders WHERE id_client=3 and id_agent=1) WHERE id_product=$idp 

There selects the name of the table, and from it displays information

In orders there is a list of tables, so I substitute them. and from each table I take data.

  • maybe so? SELECT * FROM (SELECT table_name, id_product FROM orders WHERE id_client = 3 and id_agent = 1) WHERE id_product = $ idp - mountpoint
  • This is logically wrong. Your query will be identical with "SELECT table_name FROM orders WHERE id_client=3 and id_agent=1 and id_product=" .$idp . You need to expand this query into two. In oracle there is a dynamic sql for this, in mysql as far as I know not - cadmy
  • If you really want to use so much indigestible architecture. `` `$ table = $ db-> queryScalar ('SELECT table_name FROM orders WHERE id_client = 3 and id_agent = 1'); $ query = 'SELECT * FROM'. $ table. ' WHERE id_product = '. $ Idp; $ data = $ db-> queryAll ($ query); `` `In place of muscle, I would also refuse to work, at least for the reason that one hundred tables can return. - etki
  • Reinstate the brain-on-crutches tag. - Yura Ivanov

1 answer 1

At the output of the subquery, one column is returned - table_name. Therefore condition

 WHERE id_product=$idp 

will simply result in an "unknown id_product" error.

  • @msi, the author wants to pull out the table with a query and immediately shove it from the next query - etki
  • SELECT * FROM (SELECT table_name, id_product FROM orders WHERE id_client = 10 AND id_agent = 2) AS tb Returns from 0 - 100 tables, and you need to select all the fields from those tables in turn (the fields are the same), but I only get the table names, but 2nd request to select all fields from these names does not work anymore - Man
  • @ Yura Suchko, lol? How in one from it it is possible to push hundred tables? - etki 2:53 pm
  • This is a maximum of 100, as well as 1-10, and in turn to take information from them, given that their structure is identical, only the records - Man
  • 2
    Create a cursor based on a subquery. Go around it in a loop, and dynamically execute the query for each table. - msi