I'm relatively new to SQL and I've been having trouble trying to figure out how to select rows from a table that matches a string. The name of the table uses the current month and year. It looks like this:
XXXX.xxx_YY_MM
where XXXX is the database, YY is year, and MM is month. So normally the query would just look like this:
select * from XXXX.xxx_16_05;
However, I want it to be able to change depending on the date. So I thought this would work:
select * from (select concat('XXXX.xxx_',date_format(now(), '%y_%m'))));
The concat bit gives me something that looks exactly like the name of the table. But it doesn't work and I'm not sure why. It says every table must have it's own alias. I'm not sure what to do about it.
Alternatively, I was thinking maybe something like this would be ok
select * from (select * from information_schema.tables where table_name like concat('%logsms_',date_format(now(), '%y_%m'),'%'));
But it doesn't work either. What should I do? There is only one table with a name that matches the string.