Andrea
Andrea

Reputation: 79

SQL query Join with datetime inside

I need to manage some data and retrieve a result I'm not able to do.

Table1
id int
idcat int (related to the table2)
tt varchar
dt datetime

Table2
idcat int
days int

what I need is to retrieve ALL table1 record that are not older than days related to each category (idcat).

Thanks, A.

Upvotes: 0

Views: 82

Answers (2)

Milan Halada
Milan Halada

Reputation: 1934

SELECT * from Table1 a join Table2 b on a.idcat = b.idcat where DATEDIFF(NOW(), dt)<=b.days

should give you all records that are not older than days in each category

Upvotes: 1

Murtaza
Murtaza

Reputation: 3065

I am not sure whether my below query will work but this is how the logic need to be implemented. I have modified my SQL Server Query to MySQL therefore I am not sure.

SELECT * from Table1 a join Table2 b on a.idcat = b.idcat where DATE_ADD(NOW(), INTERVAL b.days DAY)

You can use DATE_ADD() function try the below Query.

SELECT DATE_ADD('2008-12-15', INTERVAL b.days DAY)

Upvotes: 0

Related Questions