How to make sql timestamp as column name?

For instance:

select count(*) as (select date_sub(curdate(),interval 4 day)) from userinfo where createTime > (select date_sub(curdate(),interval 4 day)); 

This does not work. He says that the syntax after the "how" is incorrect. How to do it?

I want the result to be like this:

 | |2016-01-14| |-|----------| |1| 1000 | 
+5
source share
1 answer

With a regular static query, you cannot define the column name as a variable / subquery, but you could achieve it using 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 ║ ╚════════════╝ 
+4
source

Source: https://habr.com/ru/post/1240834/


All Articles