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

  • Home
  • SEARCH
  • 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 7719415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:24:38+00:00 2026-06-01T03:24:38+00:00

Example table content ‘main’ | id | total | | 1 | 10 |

  • 0

Example table content

'main'
| id | total |
| 1  | 10    |
| 2  | 20    |
'timed'
| id | id_main | date_from  | date_to    | total |
| 1  | 2       | 2012-03-29 | 2012-04-29 | 50    |

Desired result

| id | total |
| 1  | 10    |
| 2  | 50    |

Not exactly working query

SELECT main.id AS id, COALESCE(timed.total, main.total) AS total
FROM main
LEFT JOIN timed
ON main.id = timed.id_main
WHERE SYSDATE() BETWEEN timed.date_from AND timed.date_to

Result

| id | total |
| 2  | 50    |

In tables ‘main’ and ‘timed’ ‘total’ field will never be NULL.
In some ‘timed’ records there will be no relative ‘id_main’, or there will be few, but they will differ, ‘date_from’ ‘date_to’ never intersect.
Table ‘main’ is large, but in ‘timed’ will always be two or three relative records.

CREATE TABLE `main` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `total` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `main` VALUES (1,10);
INSERT INTO `main` VALUES (2,20);
CREATE TABLE `timed` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_main` int(11) unsigned NOT NULL DEFAULT '0',
  `date_from` date DEFAULT NULL,
  `date_to` date DEFAULT NULL,
  `total` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`),
  KEY `link` (`id_main`)
) ENGINE=InnoDB;
INSERT INTO `timed` VALUES (1,2,'2012-03-29','2012-03-30',50);
ALTER TABLE `timed`
ADD CONSTRAINT `link` FOREIGN KEY (`id_main`) 
REFERENCES `main` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Sorry for my english.

  • 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-01T03:24:40+00:00Added an answer on June 1, 2026 at 3:24 am

    You should move the date condition in the join condition:

    SELECT main.id AS id, COALESCE(timed.total, main.total) AS total
    FROM main
    LEFT JOIN timed
    ON main.id = timed.id_main and SYSDATE() BETWEEN timed.date_from AND timed.date_to
    

    In your query, those rows not matched are filtered out by the WHERE condition because timed.date_form and timed.date_to are null, so sysdate can’t be between them 🙂

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

Sidebar

Related Questions

The BigTable article main example is storing web content in a big table. Is
For the sake of the example, consider a table create table foo ( contents
Example: Table: box boxID color 01 red 02 blue 03 green Table: boxHas boxID
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
This is the example table: Column | 1st record | 2nd record | 3rd
I have a SQL table like so: Update: I'm changing the example table as
In my database I have tables that define types for example Table: Publication Types
Ok, I have an example table with the following information and query. First up
Do you prefix each field in a table with abbreviated table name? Example: Table:
I have two tables, for example: Table A Table B ======= ======= Name |

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.