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;
Related
In Mysql, I am tring to find a way to pass a column value into a variable. Then use the variable as a table name in another query...Below is a MsSQL version of it, Please help me find a Mysql equivalent.
declare #tblname1 varchar(400)
set #tblname1=(SELECT companyname from companies where id=5)
exec(' SELECT sh.streetname FROM '+#tblname1+' sh WHERE sh.id IN (SELECT id from allstreets)')` `
Take a look at the following: https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html
set #tblname1=(SELECT companyname from companies where id=5);
PREPARE stmt1 FROM CONCAT('SELECT sh.streetname FROM ', #tblname1, ' sh WHERE sh.id IN (SELECT id from allstreets)');
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Also, based on your question, I must say that creating a table for each companyname in the company table is a poor design and can only lead to frustration and disappointment.
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 try to use the following code to empty a database (delete all tables) :
SELECT concat('DROP TABLE IF EXISTS ','`',table_schema,'`','.','`',table_name,'`',';') FROM information_schema.tables WHERE table_schema = 'DB';
I am getting an output of the commands, but nothing happens to the database. If I take an individual command from the output, and run it in the console, it works.
What am I doing wrong?
As you know, SELECT only returns the result of a query. It doesn't know you actually intend to execute the result of that query. (In most cases, that would make no sense.) You can use prepared statements to do what you want (untested):
SET #s:='';
SELECT #s:=concat(#s, 'DROP TABLE IF EXISTS ','`',table_schema,'`','.','`',table_name,'`',';') FROM information_schema.tables WHERE table_schema = 'DB';
PREPARE stm FROM #s;
EXECUTE stm;
DEALLOCATE PREPARE stm;
Why you're using SELECT statement, Simply try this query:
DROP TABLE IF EXISTS table_name;
DROP TABLE (
SELECT table_name
FROM information_schema.`TABLES`
WHERE table_schema = 'myDatabase' AND table_name LIKE BINARY 'del%');
I know this doesn't work! What is the equivalent for something like this in SQL? I can whip out a simple Python script to do this but was just wondering if we can do something with SQL directly. I am using MySQL. Thank you!
You can use prepared statements -
SET #tables = NULL;
SELECT GROUP_CONCAT('`', table_schema, '`.`', table_name,'`') INTO #tables FROM information_schema.tables
WHERE table_schema = 'myDatabase' AND table_name LIKE BINARY 'del%';
SET #tables = CONCAT('DROP TABLE ', #tables);
PREPARE stmt1 FROM #tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
It will generate and execute a statement like this -
DROP TABLE myDatabase.del1, myDatabase.del2, myDatabase.del3;
A minor improvement to #Devart's answer:
SET #tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.`', table_name, '`') INTO #tables FROM
(select * from
information_schema.tables
WHERE table_schema = 'myDatabase' AND table_name LIKE 'del%'
LIMIT 10) TT;
SET #tables = CONCAT('DROP TABLE ', #tables);
select #tables;
PREPARE stmt1 FROM #tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
This script should be executed repeatedly until the console's output is NULL
The changes are:
backtick (`) wrapping the table name (if it contains non standard characters)
added a LIMIT to avoid the truncation issue I commented about
added a "print" (select #tables;) to have some kind of control when to stop executing the script
If you just need to quickly drop a bunch of tables (not in pure SQL, so not directly answering this question) a one line shell command can do it:
echo "show tables like 'fsm%'" | mysql | tail +2 | while read t; do echo "drop table \`$t\`;"; done | mysql
I found it useful to add an IFNULL to Devart's solutions to avoid generating an error if there are no tables matching the query.
SET #tables = IFNULL(CONCAT('DROP TABLE ', #tables),'SELECT NULL;');
In addition to #Devart's answer:
If you have many tables, you may need to set group_concat_max_len:
SET group_concat_max_len = 4096;
You can do this quickly and easily if you have phpMyAdmin available and the requirement is to drop tables with a specific prefix. Go to the database you want, and show the list of all the tables. Since tables are shown in alphabetic order, the tables with the prefix for deletion will all appear together. Go to the first one, and click the tick box on the left hand side. Then scroll down to the last table with the prefix, hold down shift, and click the tick box. That results in all the tables with the prefix being ticked. Go to the bottom of the list, and select the action drop for all selected tables. Go through with the deletion, checking that the SQL generated looks right!