Select day of week from date

后端 未结 3 1915
借酒劲吻你
借酒劲吻你 2020-12-29 04:14

I have the following table in MySQL that records event counts of stuff happening each day

event_date     event_count
2011-05-03     21
2011-05-04     12
2011         


        
3条回答
  •  时光说笑
    2020-12-29 04:33

    Use DAYOFWEEK in your query, something like:

    SELECT * FROM mytable WHERE MONTH(event_date) = 5 AND DAYOFWEEK(event_date) = 7;
    

    This will find all info for Saturdays in May.

    To get the fastest reads store a denormalized field that is the day of the week (and whatever else you need). That way you can index columns and avoid full table scans.

    Just try the above first to see if it suits your needs and if it doesn't, add some extra columns and store the data on write. Just watch out for update anomalies (make sure you update the day_of_week column if you change event_date).

    Note that the denormalized fields will increase the time taken to do writes, increase calculations on write, and take up more space. Make sure you really need the benefit and can measure that it helps you.

提交回复
热议问题