Mysql select from all tables

I have a mySQL database called listDB that contain several tables with column name Product etc. I want to SELECT from all tables where Product Like %XYZ%, and display the search result in a separate table.

I tried this:

SELECT * FROM * WHERE Product LIKE %XYZ%

But it is not working. What is the right query for this purpose?

John Woo

252k68 gold badges491 silver badges486 bronze badges

asked Feb 18, 2013 at 6:58

Farid-ur-RahmanFarid-ur-Rahman

1,7418 gold badges30 silver badges45 bronze badges

4

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.

answered Feb 18, 2013 at 7:07

CloudyMarbleCloudyMarble

36.3k70 gold badges94 silver badges128 bronze badges

1

SELECT product FROM Your_table_name WHERE Product LIKE '%XYZ%';

The above statement will show result from a single table. If you want to add more tables then simply use the UNION statement.

SELECT product FROM Table_name_1 
WHERE Product LIKE '%XYZ%'  
UNION  
SELECT product FROM Table_name_2 
WHERE Product LIKE '%XYZ%'  
UNION  
SELECT product FROM Table_name_3 
WHERE Product LIKE '%XYZ%' 

... and so on

bummi

26.9k13 gold badges61 silver badges100 bronze badges

answered Sep 25, 2013 at 17:16

As Suhel Meman said in the comments:

SELECT column1, column2, column3 FROM table 1
UNION
SELECT column1, column2, column3 FROM table 2
...

would work.

But all your SELECTS would have to consist of the same amount of columns. And because you are displaying it in one resulting table they should contain the same information.

What you might want to do, is a JOIN on Product ID or something like that. This way you would get more columns, which makes more sense most of the time.

answered Feb 18, 2013 at 7:07

JensJens

2,0001 gold badge13 silver badges30 bronze badges

why you dont just dump the mysql database but with extension when you run without --single-transaction you will interrupt the connection to other clients:

mysqldump --host=hostname.de --port=0000 --user=username --password=password --single-transaction --skip-add-locks --skip-lock-tables --default-character-set=utf8 datenbankname > mysqlDBBackup.sql 

after that read out the file and search for what you want.... in Strings.....

answered Mar 20, 2013 at 19:09

Improve to @inno answer

delimiter //

DROP PROCEDURE IF EXISTS get_product;  
CREATE PROCEDURE get_product[]
BEGIN
   DECLARE i VARCHAR[100]; 
   DECLARE cur1 CURSOR FOR SELECT DISTINCT table_name FROM information_schema.columns WHERE COLUMN_NAME IN ['Product'];
   OPEN cur1;

   read_loop: LOOP
       FETCH cur1 INTO i;
    
       SELECT i; -- printing table name
    
       SET @s = CONCAT['select * from ', i, ' where Product like %XYZ%']; 
       PREPARE stmt1 FROM @s;
       EXECUTE stmt1;
       DEALLOCATE PREPARE stmt1;

   END LOOP read_loop;

   CLOSE cur1;
END//

delimiter ;

call get_product[];

answered Oct 20, 2020 at 16:26

Duong LeDuong Le

1781 silver badge5 bronze badges

You can get all tables that has column "Product" from information_Schema.columns

SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name ="Product";

Nor create a procedure

delimiter //
CREATE PROCEDURE curdemo[]
BEGIN
  DECLARE a varchar[100]; 
  DECLARE cur1 CURSOR FOR SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name ="Product";
  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO a;

    SELECT * FROM a;

  END LOOP;

  CLOSE cur1;
END;

delimiter ;

call curdemo[];

answered Feb 18, 2013 at 7:14

iiroiiro

3,0701 gold badge18 silver badges22 bronze badges

1

Can you SELECT all from multiple tables in SQL?

In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables.

How do I get a list of table names in MySQL?

The syntax to get all table names with the help of SELECT statement. mysql> use test; Database changed mysql> SELECT Table_name as TablesName from information_schema. tables where table_schema = 'test'; Output with the name of the three tables.

How can I get data from all tables in MySQL?

The syntax is as follows..
SELECT SUM[TABLE_ROWS] FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = 'yourDatabaseName'; ... .
mysql> SELECT SUM[TABLE_ROWS] ->FROM INFORMATION_SCHEMA. TABLES ->WHERE TABLE_SCHEMA = 'business'; ... .
mysql> SELECT table_name, table_rows ->FROM INFORMATION_SCHEMA..

How do I SELECT all tables?

Then issue one of the following SQL statement:.
Show all tables owned by the current user: SELECT table_name FROM user_tables;.
Show all tables in the current database: SELECT table_name FROM dba_tables;.
Show all tables that are accessible by the current user:.

Chủ Đề