Dynamic table name based on date - mysql

I have some tables which have dates in it.
How can I query on them based on the dates. There are multiple tables with prefix_date format.
I mean if the name of the table is foo and today's date being 2016 06 09. The table I need to access will be foo_2016_06_09.
I need to run this query daily on the day before date. Is this possible? If yes how?

DATE_FORMAT(NOW(),'%Y_%m_%d') will return as 2016_06_09, you can concatenate the remain table name part and use as dynamic query:
SET #table_name:='foo_';
SET #date_part:=DATE_FORMAT(NOW(),'%Y_%m_%d');
SET #sql:=CONCAT('SELECT * FROM ', #table_name, #date_part);
PREPARE dynamic_statement FROM #sql;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
For the previous day, use DATE_SUB
SET #date_part:=DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y_%m_%d');

Related

Get specifi field value from a dynamic SQL in MYSQL

I am trying to create and execute sql dynamically in MYSQL. I have a field myColumn and i want to show/get the value of this field. The below script will get me all data in the table,
SET #value:='myColumn';
SET #sql:=CONCAT("SELECT * FROM change c where c.field='",#value"'");
PREPARE dynamic_statement FROM #sql;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
The reason i want to do this is because I want to work on that specific field. Like execute a function on the field for e.g. repalce,substring etc etc. I know i can do any work inside #sql but i want to know if i can access the columns once the sql is executed.
select #sql.myColumn will not work. How can i achieve this?

How to write generic code for union tables over years?

I have an Hive ETL job where I have to extract data from yearly tables and union them. Don't ask why there is a separate table for each year (legacy systems and huge size).
Lets assume table names are table11, table12, . . ., table19
Now I can write query upto 'from' table19, but I want to write generic code, otherwise the code have to be updated every year. I believe one can't use wildcards in the 'from' clause, if I am correct. e.g. table20*
Best Regards,
you can use prepared statement. So you can generate a query with CONCAT and then execute it.
-- SELECT CONCAT("insert into newtable select * from table",DATE_FORMAT(now(),'%y'))
SELECT CONCAT(" select * from mysql.user") INTO #sql;
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

How to describe table that I only have part of name?

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;

Mysql sum all columns starting with some letters

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;

SQL - Select rows that contain a NULL value in a non-nullable column

I'm hoping somebody can help me with a script / query, the target DB is mySQL.
The database I am working with does not conform to it's own constraints and is in the process of being moved to MS SQL. What I am looking to find is a query that can be run against a table which looks for rows that contain a null value in a column that does not allow nulls, which in turn will assist with SSIS DFT debugging times.
Many thanks.
Try:
SELECT group_concat(`COLUMN_NAME`) as myList
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename'
-- AND `IS_NULLABLE`='NO'
into #colname;
SET #query = CONCAT('SELECT ',#colname,' FROM yourtablename');
PREPARE stmt FROM #query;
EXECUTE stmt;