开发者

Creating Tempory column in MySQL

In my database(MySQL) there is a field that holds birthday. When I retrieving records I want to print there age doing like this age = (YEAR(CUERRENT_DATE) - YEAR(birthday)). But in my database there is no column called age. So I just want to do, create temporary column and hold the age, and 开发者_如何学Cwhen I retrieving records I can print age also. Is there a way to do this? How can I write the query? I am new to SQL. Please help me.


You don't need a temporary column, simply select:

SELECT YEAR(CUERRENT_DATE) - YEAR(birthday) AS age, otherColumns FROM xyz WHERE ...

As you see, I used AS for renaming the expression to your "temporary column". It will then appear in your result set.


You can compute the age directly in SELECT, and return it like if it was a column:

SELECT *, YEAR(CUERRENT_DATE) - YEAR(birthday) as age
FROM table
...

This is like a temporary column.


I guess you do not need to store age - you retrieve the birth date from SQL and calculate the age in code and print. Aslo you can use DB inbuilt functions to get the current date - if you want to do that in SQL.

If you are using MySQL.

SELECT (YEAR(CURDATE())-YEAR(birth)) AS age FROM TABLE_NAME;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜