Search code examples
mysqlphpmyadminwampserver

Which sql query to be used?


I have say n different tables each with 3 attributes

ie Table1: name|price|id
   Table2: name|price|id
   .
   .
   Tablen: name|price|id

Pls tell me what sql query i should use in my PHP code so that i can retrieve all the names and prices from all the above tables having the same id say id=1.


Solution

  • You can use the SELECT with UNION if u have same numbers of column.

    Example :

    SELECT * FROM Table1
    UNION
    SELECT * FROM Table2
    UNION
    SELECT * FROM Tablen
    

    if u wan't in your results all record use UNION ALL if u want only differences use UNION ALL

    IN YOUR CASE THIS IS SOLUTION FOR YOU:

    SELECT * FROM
    (
    
      select * from table1
      UNION
      select * from table2
      UNION
      select * from table3
    
    ) AS Results
    WHERE Results.ID = 1