简体   繁体   中英

Data Searching Week/Month Wise In MYSQL

I am trying to search data Week and month wise. We have date range in our output data. We can't use WEEK & Month function because we have previous years data. Is their any work around if i select this week then find data for the current week.

select * from tab where week(current_date) between week(start_date) and week(end_date)

above command will not provide correct information when start_date_week (51) and end_date_week (15).

Please find output/criteria:

在此处输入图片说明

I solved this problem through this technique:

1) create a permanent table (day_month_years) and insert days information for current month (because my max requirement is for Month)

2) Then find values for month case: select * from day_month_years my where CAST(my. source_name AS DATE) BETWEEN DATE_FORMAT(p_date,'%Y-%m-01') AND LAST_DAY(p_date)

3) Then place left outer join:

select *
FROM day_month_years my
    LEFT OUTER JOIN 
        (
        SELECT  p.pass_id,
                p.Last_Status,
                p.last_date_time,
                p.start_date,
                p.end_date
        FROM vw_pass_info p
        WHERE p.client_id= COALESCE(p_client_id,p.client_id)
        AND   IF(p_location_id IS NULL,p.location_id=p.location_id OR p.location_id IS NULL,FIND_IN_SET(p.location_id,p_location_id))
        ) m ON  CAST(my.source_name AS DATE) BETWEEN DATE(m.start_date) AND DATE(m.end_date) -- key section

hopes it will help you. cheers & thannks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM