sql - Selecting rows that are within a date range based on a date from another table. (MYSQL) -


i have 2 tables, share key link two. table has date column (of format mm/dd/yyyy), , table b has date field of format (yyyy-mm-dd hh:mm:ss).

what need select in table b, have key matching table , date field within 30 days of date field found in table a.

edit: both variables varchars, here have (error using alias formattedeffective in join). think below work, if use aliases in way.

select *,  date_format(str_to_date(`eff_date`, '%m/%d/%y'), '%y-%m-%d') formattedeffective  `customers`     right join `dispatch` on `customers`.`member_no` = `dispatch`.`member_no`          , `dispatch`.`sortdate` > formattedeffective          , `dispatch`.`sortdate` < date_add(formattedeffective,interval 30 day) 

what community asking ability create scenario give definitive answer question (create table statements, sample data, etc..). approach below speculation.

the assumption query makes eff_date string , sortdate stored mysql date (i.e., date, datetime, timestamp).

select d.*,        str_to_date(c.eff_date, '%m/%d/%y') ) formattedeffective    customer c   join dispatch d on (   d.member_no = c.member_no                      , d.sortdate between str_to_date(c.eff_date, '%m/%d/%y')                                         , str_to_date(c.eff_date, '%m/%d/%y')  + interval 30 day ); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -