For instance:
select count(*) as (select date_sub(curdate(),interval 4 day))
from userinfo
where createTime > (select date_sub(curdate(),interval 4 day));
This is not working. It says the syntax after 'as' is not correct. How do I make this work?
I want the result to be like this:
| |2016-01-14|
|-|----------|
|1| 1000 |
With normal static query you cannot define column name as variable/subquery, but you could achieve it with dynamic SQL(prepared statements):
SET @sql =
CONCAT('select count(*) as `',(select date_sub(curdate(),interval 4 day)),'` from userinfo where createTime > (select date_sub(curdate(),interval 4 day));');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SqlFiddleDemo
Output:
╔════════════╗
║ 2016-01-14 ║
╠════════════╣
║ 2 ║
╚════════════╝
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With