MySQL date_add, date subquery

1k views Asked by At

I am trying to retrieve the products (tbl_product) which have an offer going on from tbl_offers

columns in tbl_offers

offerid,productid,discountrate,dateadded,days

where offerid and productid are auto-incremented ids, discountrate is int, dateadded timestamp and days int.

What it basically means an offer is valid for say 10 days after it is added. Days can have different values.

the following is the query i am trying:

select a.* from tbl_product a, tbl_offers b 
where a.productid=b.productid and 
date_add(dateadded, interval (select days from tbl_offers) day)>NOW();

throws an error saying subquery returns more than one row.

I am confused as to how to target my query to get days from the subquery

1

There are 1 answers

0
Abhik Chakraborty On BEST ANSWER

You do not need sub-query and using join it could be done

select 
a.*
from 
tbl_product a
join tbl_offers b  on a.productid=b.productid
where
date_add(b.dateadded, interval b.days day ) > now() ;