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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:00:54+00:00 2026-06-07T08:00:54+00:00

I have 3 tables: file_list => list of all files on web server stats_web

  • 0

I have 3 tables:

file_list => list of all files on web server  
stats_web => number of pageviews from public web  
stats_reg => number of pageviews from "registered users" 

I have the schema and some sample data on http://sqlfiddle.com/#!2/98c6a/1/0

I am trying to calculate the date that a file was first hit either by the public or by a registered user.

Current SQL looks like this:

SELECT
    list.path,
    list.mod_date,
    IF(MIN(ifnull(web.hit_date, 0000-00-00)) < MIN(ifnull(reg.hit_date, 0000-00-00)), MIN(ifnull(web.hit_date, 0000-00-00)), MIN(ifnull(reg.hit_date, 0000-00-00))) AS 'min_date',
    IF(MAX(ifnull(web.hit_date, 0000-00-00)) > MAX(ifnull(reg.hit_date, 0000-00-00)), MAX(ifnull(web.hit_date, 0000-00-00)), MAX(ifnull(reg.hit_date, 0000-00-00))) AS 'max_date',    
    SUM(ifnull(web.pages, 0)) + SUM(ifnull(reg.pages, 0)) AS 'page_views'
FROM
    file_list list
    LEFT JOIN
         stats_web web ON list.path = web.path
    LEFT JOIN
         stats_reg reg ON list.path = reg.path
WHERE
list.path LIKE '/web/htdocs/%'
GROUP BY list.path;

The problem is that if a record appears in only one of the stats tables the min date is always 0.
This is because of the ifnull() on the MIN and MAX but if I don’t use ifnull() then both min and max dates return NULL.

  • 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-07T08:00:56+00:00Added an answer on June 7, 2026 at 8:00 am

    You should use a date in the distant future for the second argument of ifnull when it is used to compare the minimum. Otherwise, the first null would make 0000-00-00 the minimum, and since it’s the earliest possible date, no other date would be able to beat it to become the minimum.

    If you change the forth line of your query to

    IF(MIN(ifnull(web.hit_date, 9999-12-31)) < MIN(ifnull(reg.hit_date, 9999-12-31)), MIN(ifnull(web.hit_date, 9999-12-31)), MIN(ifnull(reg.hit_date, 9999-12-31))) AS 'min_date'
    

    you should get correct results.

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

Sidebar

Related Questions

I have this batch file that outputs a list of all the tables to
I have a csv file from which I have to populate different tables in
I have a .sql file from a MySQL dump containing tables, definitions and data
I have a list of strangely encoded files: 02 - Charlie, Woody and You/Study
So far all the serialization examples I have found on the web are related
Say I have four tables, users , contacts , files , and userfiles .
In my project we have a web based tool where it collects all the
I have two long list, one from a log file that contains lines formatted
I am writing a function which would get all files from a particular directory
I have the following code on my web-page. It iterates a list, creates new

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.