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 7031197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:46:17+00:00 2026-05-28T00:46:17+00:00

i was triyng for some days a way to do this using only MySQL

  • 0

i was triyng for some days a way to do this using only MySQL (and not MySQL + PHP), but im not very good with SQL querys. The idea is this, my table (a short version):

enter image description here

Any time a user comes to my site a row is added to this table including the timedate of the visit and the type of visit (for this particular problem i only need type ‘profile_visit’. I need to feed a chart with the total visits made on the current week by day.

So, i need to retrive an array something like Mon(2) Tue(0) Wed(1) and so on. Do you think is possible to do this using just MySQL querys? Thanks for any help!

  • 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-05-28T00:46:17+00:00Added an answer on May 28, 2026 at 12:46 am
    SELECT
      COUNT(DAYOFWEEK(`datetime`) = 1 OR NULL) AS Sun,
      COUNT(DAYOFWEEK(`datetime`) = 2 OR NULL) AS Mon,
      COUNT(DAYOFWEEK(`datetime`) = 3 OR NULL) AS Tue,
      COUNT(DAYOFWEEK(`datetime`) = 4 OR NULL) AS Wed,
      COUNT(DAYOFWEEK(`datetime`) = 5 OR NULL) AS Thu,
      COUNT(DAYOFWEEK(`datetime`) = 6 OR NULL) AS Fri,
      COUNT(DAYOFWEEK(`datetime`) = 7 OR NULL) AS Sat
    FROM atable
    WHERE `type` = 'profile_visit'
      AND `datetime` >= CURRENT_DATE() - INTERVAL (DAYOFWEEK(CURRENT_DATE()) - 1) DAY
      AND `datetime` <  CURRENT_DATE() + INTERVAL 1 DAY
    

    Instead of

    COUNT(DAYOFWEEK(`datetime`) = 1 OR NULL)
    

    you can also use

    SUM(DAYOFWEEK(`datetime`) = 1)
    

    I tend to use COUNT when it’s about counting, but that is merely a personal preference. There is no difference in terms of results returned by either method, and should be none either in terms of performance.

    The OR NULL part of the COUNT expression is explained here:

    • Why do I need "OR NULL" in MySQL when counting rows with a condition

    UPDATE

    For weeks starting on Monday try the following equivalent of the above script:

    SELECT
      COUNT(WEEKDAY(`datetime`) = 0 OR NULL) AS Mon,
      COUNT(WEEKDAY(`datetime`) = 1 OR NULL) AS Tue,
      COUNT(WEEKDAY(`datetime`) = 2 OR NULL) AS Wed,
      COUNT(WEEKDAY(`datetime`) = 3 OR NULL) AS Thu,
      COUNT(WEEKDAY(`datetime`) = 4 OR NULL) AS Fri,
      COUNT(WEEKDAY(`datetime`) = 5 OR NULL) AS Sat,
      COUNT(WEEKDAY(`datetime`) = 6 OR NULL) AS Sun
    FROM atable
    WHERE `type` = 'profile_visit'
      AND `datetime` >= CURRENT_DATE() - INTERVAL ( WEEKDAY(CURRENT_DATE()) ) DAY
      AND `datetime` <  CURRENT_DATE() + INTERVAL 1 DAY
    

    References:

    • DAYOFWEEK() (MySQL)

    • WEEKDAY() (MySQL)

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

Sidebar

Related Questions

I'm trying to troubleshoot this problem using SQL Profiler (SQL 2008). After days of
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
Ok so I have been trying to fix this for days, and I'm not
Possible Duplicate: How to find number of days between two dates using php I'm
This is a nut I'm cracking these days Application I'm working on has some
Recently I was trying some practice programs in python and I came across this
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to
I've searched for two days trying to find a solution to this. My problem
Hi guys I'm using this wonderful class here to do a bit of code
I've spent two days on this and I still can't find the cause of

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.