Select From all tables - MySQL

前端 未结 6 420
灰色年华
灰色年华 2020-12-10 10:54

I have a mySQL database called listDB that contain several tables with column name Product etc. I want to SELECT from all tables where

6条回答
  •  伪装坚强ぢ
    2020-12-10 11:45

    You get all tables containing the column product using this statment:

    SELECT DISTINCT TABLE_NAME 
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE COLUMN_NAME IN ('Product')
            AND TABLE_SCHEMA='YourDatabase';
    

    Then you have to run a cursor on these tables so you select eachtime:

    Select * from OneTable where product like '%XYZ%'
    

    The results should be entered into a 3rd table or view, take a look here.

    Notice: This can work only if the structure of all table is similar, otherwise aou will have to see which columns are united for all these tables and create your result table / View to contain only these columns.

提交回复
热议问题