Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:49:47+00:00 2026-06-17T14:49:47+00:00

I have the following query. SELECT COUNT(*), WEEK(date), YEAR(date) FROM myTable GROUP ON YEAR(date),

  • 0

I have the following query.

SELECT COUNT(*), WEEK(date), YEAR(date) FROM myTable GROUP ON YEAR(date), WEEK(date)

Say it produces the following results

32 33 2012
43 34 2012
39 35 2012
17 36 2012

I now want to get all 39 records in week 35 of 2012. I do not, however, wish to use WEEK(date)=35 AND YEAR(date)=2012 in my WHERE clause as it does not utilize indexes. Instead, I wish to find the boundaries and use conditionals. I also do not want to use BETWEEN as rounding errors might occur.

I therefore try the following thinking all is good, but do not get 39 records. Obviously MySQL and PHP deal differently with weeks. I see that MySQL WEEK() utilizes mode 0, 2, 4, and 6 that all return a week that starts with Sunday. Ideally, I would have the one that is most commonly used by people, the most important thing is that it is the same as provided by DateTime. How would I do so? Thank you

$w=35;$y=2012;   //Given
$w=sprintf('%02d',$w);    //Make sure it is two digits
$date = new DateTime($y.'W'.$w);
$d1=$date->format('Y-m-d');
$date->add(new DateInterval('P1W'));
$d2=$date->format('Y-m-d');
$sql='SELECT * FROM myTable WHERE date >= ? AND date < ?';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:49:48+00:00Added an answer on June 17, 2026 at 2:49 pm

    You are on the right track with regards to how MySQL works, having various modes for week-related functions that can produce different results. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

    It is my understanding the MySQL mode which is equivalent to PHP’s date logic is mode 3, which is the ISO week date standard http://en.wikipedia.org/wiki/ISO_week_date

    This has weeks starting on Mondays and weeks numbered 1-53.

    So you would need to use WEEK(date_field, 3) to get PHP compatible values.

    As an alternate approach, one thing I have found handy in cases where you need the ability to flexibly query on different date ranges or aggregations (Q1- Q4, H1 – H2, etc.), or where you might need to utilize different weeks than PHP supports is to use a date helper table in MySQL (similar to what one may use as a date dimension table in a data warehouse, if you are familiar with that). It can give you a convenient table to join against in order to look up date ranges. Something like this should work:

    http://databobjr.blogspot.com/2012/06/create-date-dimension-table-in-mysql.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
I have the following hive query: select count(distinct id) as total from mytable; which
I have the following query: SELECT timestamp, COUNT(*) FROM table GROUP BY timestamp But
I have the following query: select count(ords.TRACKING_NUM) from Orders ords (NoLock) group by ords.TRACKING_NUM
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I have the following query SELECT Count(*) as Total_Count, Col1 FROM Table1 GROUP BY
Let say that we have the following query: SELECT DISTINCT COUNT(`users_id`) FROM `users_table`; this
I have the following query: $count = (SELECT COUNT(*) FROM post GROUP BY ID
I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have following rather simple query select count(*) from tbl t1, tbl t2 For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.