I have a table of tables called TABLES. TABLES has a column TABLE_NAME.
For each TABLE_NAME there exists a table in the database. All of these tables are structured the same.
Ultimately I would like to UNION columns from each of these tables. But I'm not getting close to that objective.
I am stuck on the following query:
SELECT column_name FROM (SELECT table_name FROM TABLES) T;
The error returned is "ERROR 1054 (42S22): Unknown column 'column_name' in 'filed list'.
I haven't seen a good representation of SELECT...FROM (SELECT...FROM)
Any help is most appreciated.
There is no way to achieve this with a static query. If you really need this for some reason you have to use dynamic SQL
SET #sql = NULL;
SELECT GROUP_CONCAT(CONCAT('SELECT column_name FROM `', table_name, '`') SEPARATOR ' UNION ALL ')
INTO #sql
FROM tables;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo
To simplify things on the calling side you can always wrap it in a stored procedure.
You can try
SELECT column_name FROM (SELECT column_name FROM TABLES) T;
OR
SELECT column_name FROM (SELECT * FROM TABLES) T;
Related
I have an old script that creates each day a table and store data in it.
The table names are like this today-date-some-other-string, i.e : 02-02-2018-other-string.
The question is, how could I describe the structure of that table despite I only have today's date? I mean is there a way to do something like this :
DESC WHERE Table like "02-02-2018%"
Thank you.
In mysql you could use prepared statements for example
set #sql = concat('describe ' , (select table_name
from information_schema.tables
where table_name like 'users' and table_schema = 'sandbox')
,';');
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
I can write a query to search for a table that has a particular column in a DB
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
but My Question is:
can I search an entire DB for a value in a column?
So I'm unsure the name of the column and I am unsure the name of the DB table but I know the value is 'Active'
Yes, you can. In that case, you need to prepare dynamic query once you get list of tables, which consists column, which actually you are looking for.
Now create a cursor for
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
Using above cursor loop below
SET #s = CONCAT("select count(*) from [tablename] where [columnname] like ","'%SOMETHING%'");
PREPARE stmt FROM #s
execute stmt;
DEALLOCATE PREPARE stmt;
I am trying add one column in my Mysql database that sums all the columns starting by 'tokenvalid' which can take the value of 1 or 0.
And let's say I have 50 columns like that in my database (i.e. tokenvalid1, tokenvalid2 ...., tokenvalide50) with other columns between.
Please find below the code I would like to implement. I know that is not correct at all but it is just to give you an idea of what I am trying to do.
Thank you for your help!
'SELECT *, sum(column_name LIKE "tokenvalid"%) as total FROM points WHERE 1'
This post should help you. The post describes how to get the columns and then query for results.
MySQL Like statement in SELECT column_name
Something like this should help you.
SET #colname = (SELECT GROUP_CONCAT(`column_name`) from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='points' AND `column_name` LIKE 'tokenvalid%');
SET #table = 'points';
SET #query = CONCAT('SELECT SUM(',#colname,') FROM ', #table);
PREPARE stmt FROM #query;
EXECUTE stmt;
Similar to this answer by RocketDonkey
If the string is in your external application (like PHP), sure, just construct the MySQL statement.
If the string is inside a MySQL table, you can't. MySQL has no eval() or such function. The following is impossible:
Suppose you have a table 'queries' with a field "columnname" that refers to one of the column names in the table "mytable". There might be additional columns in 'queries' that allow you to select the columnname you want...
INSERT INTO queries (columname) VALUES ("name")
SELECT (select columnname from queries) from mytable
You can however work with PREPARED STATEMENTS. Be aware this is very hacky.
SELECT columnname from queries into #colname;
SET #table = 'mytable';
SET #s = CONCAT('SELECT ',#colname,' FROM ', #table);
PREPARE stmt FROM #s;
EXECUTE stmt;
I have 1200 tables in my database, and the table names of half of them are start with digits, just looks like this
mysql> SHOW TABLES;
1000_quarterly_1000
1001_quarterly_1001
...
quarterly_1000
quarterly_1001
....
And I want drop all the tables the name of which start with digits, for example table 1000_quarterly_1000 must be dropped. And since tables have been dropped from the current database and I want to store them to other database I have defined.
How can I write mysql query to do this, since I can use Python to do this, but mysql query can be better.
If you want to drop the tables in one statement, you can construct the statement as:
select concat('drop table ', group_concat(t.table_schema, '.', t.table_name separator ', '))
from information_schema.tables
where left(t.table_name, 1) between '0' and '9';
You can then try to execute a command:
declare #sql varchar(65000);
select #sql := concat('drop table ', group_concat(t.table_schema, '.', t.table_name separator ', '))
from information_schema.tables
where left(t.table_name, 1) between '0' and '9';
prepare s from #sql;
execute s;
select concat('drop table ',t.table_schema,'.',t.table_name,';') drop_ddl
from information_schema.tables t where t.table_name rlike '[0-9].'
How do i find similar column names from a database table?
for e.g.
a database table
1_1 1_2 1_3 5_6 67
| | | |
| | | |
So, 1_1, 1_2, 1_3, 5_6 and 67 are the column names of a database table. And i would like to retrieve only the column names starts with 1 (1_1, 1_2 and 1_3). i tried the sql query but it dint work..
SELECT 1 LIKE '%1%' FROM sheet1;
It shows something of this short
1 LIKE '%1%'
1
1
Documentation find here
SHOW COLUMNS FROM tbl_name FROM db_name
LIKE '1%'
To get the contents of the respective column:
SQL Fiddle
this may be useful to you
SELECT COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='table' AND TABLE_SCHEMA='database_name'
information_schema is database containing meta data about all databases so when ever you want this kind of data you simply fire query on it
Try this:
SELECT GROUP_CONCAT(COLUMN_NAME) INTO #s
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'tableName' AND
COLUMN_NAME LIKE '1%';
SET #sql = CONCAT('SELECT ', #s, ' FROM tableName');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;