Boober Bunz Boober Bunz - 2 years ago 76
MySQL Question

Calculate date difference in MySQL and PHP

Let's say we have a MYSQL database, 'employees.'

The database contains a column, "EXP," which contains a String signifying the persons START DATE... example: "2015-01-11" (formatted: yyyy-mm-dd)

I would like for database queries to return a variable "experience" which is calculated by comparing the current date to the date contained in the EXP column.

This is the most recent attempt; what I'm trying:

SELECT phone, name, (DATEDIFF('CURDATE()','EXP') AS experience, bio, photo, FROM squad

Can you tell what I am trying to accomplish? If anyone knows the correct code to put here it would be greatly appreciated.

Please note, if I simply replace the code

(DATEDIFF('CURDATE()','EXP') AS experience

with 'EXP' , then there are no errors and everything works as expected.

Answer Source

you have extra brackets AND 'EXP' will use the string literal not the db field

    SELECT phone, name, DATEDIFF(now(), exp) AS experience, bio, photo
FROM squad

working demo:!9/79e87/2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download