If you want the column name to be DATE_FORMAT(CURRENT_DATE(),'%W')
, it’s simple, just escape it:
select DATE_FORMAT(CURRENT_DATE(),'%W') as 'DATE_FORMAT(CURRENT_DATE(),\'%W\')'
If you want the column name to be something like today’s day, such as Monday
, it’s more complicated. You need to execute it at least twice. First, execute:
select DATE_FORMAT(CURRENT_DATE(),'%W')
Get the value of this SQL, which is Monday
. Then concatenate this value with:
select DATE_FORMAT(CURRENT_DATE(),'%W') as
This way, you can achieve both the column name and value being Monday
.
However, this method is highly discouraged because concatenating SQL poses a risk of SQL injection, which is a security hazard. Generally, to avoid SQL injection, prepared statements are used, like this:
prepare stmt from 'select DATE_FORMAT(CURRENT_DATE(),\'%W\') as ?';
But this is not supported and will result in an error.
If you can’t avoid string concatenation, you can’t completely eliminate the risk of SQL injection. Therefore, although the above method can achieve both the column name and value being Monday
, it is highly discouraged.