P
P
Pista2021-09-14 12:27:48
SQL
Pista, 2021-09-14 12:27:48

How to find data for all tables from a specific column?

There is a file SQLITE3 base.db
WindowsTerminal_fxzDrIgEGk.png

Contains many tables.
Each table has a referer column name, you need to search all data tables from the referer column, for example, a word containing yandex from the referer column

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-09-14
@Pista

We generate a query for all tables:

SELECT 
    GROUP_CONCAT('select "' || name || '" table_name, * from `' || name || '` where referer = "yandex"', ' union ') q
FROM 
    sqlite_master 
WHERE 
    type ='table' AND 
    name NOT LIKE 'sqlite_%';

We get:
select "t1" table_name, * from t1 where referer = "yandex" union select "t2" table_name, * from t2 where referer = "yandex"

We do:
+============+====+=========+
| table_name | id | referer |
+============+====+=========+
| t1         | 2  | yandex  |
+------------+----+---------+
| t2         | 20 | yandex  |
+------------+----+---------+

SQLite fiddle

R
Rsa97, 2021-09-14
@Rsa97

Through SHOW TABLES you get a list of all tables and in turn do a search in each or form a UNION query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question