Boober Bunz Boober Bunz - 2 years ago 64
SQL Question

SOLVED: calculate date difference in MYSQL query (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.

I have tried what seems to be a million different methods... via google, php docs, etc etc etc... but nothing seems to be working.

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, so please do not say 'POST MORE CODE.' Thanks.

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