1

I'm trying to write a query to extract the rows "not expired"

I have a database with a table in which are there :

IdActivity    lifespan (months)

And another table in which I have:

IdSubject    IdActivity    StartDate

Now I want to extract all IdSubject with not expired yet activity

I need something like (I know it's wrong: it's only to make clear what I want):

SELECT IdSubject FROM Tab2 INNER JOIN Tab1 USING(IdActivity)
WHERE DATEADD(StartDate, INTERVAL lifespan MONTS)>NOW()

I made many attempts but nothing worked!

Thanks in advance for your time.

genespos
  • 3,211
  • 6
  • 38
  • 70
  • Can't you save expire date instead of lifespan? Then you can easily compare startdate with expiredate. – Olle Härstedt Mar 20 '15 at 18:11
  • Sorry, I thought the opposite and reversing thought I solved: `SELECT IdSubject FROM Tab2 INNER JOIN Tab1 USING(IdActivity) WHERE StartDate > NOW - INTERVAL (Select lifespan ......) MONTH ` – genespos Mar 20 '15 at 18:17

1 Answers1

0

You were really close - just one character wrong: Use < NOW() instead of > NOW() to find expired dates.

SELECT IdSubject
FROM Tab2
JOIN Tab1 USING(IdActivity)
WHERE DATE_ADD(StartDate, INTERVAL lifespan MONTH) < NOW()

See SQLFiddle

Bohemian
  • 412,405
  • 93
  • 575
  • 722