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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:20:48+00:00 2026-06-19T02:20:48+00:00

According to this topic ( MySQL : Count row where sum of left day

  • 0

According to this topic (MySQL : Count row where sum of left day is less than 30) that I created before. Now I have another problem. I need those rows that going to be expired within 30 days ahead being sum. So I can tell how many product is going to be expired in a month. Here is my code:

user_db

id         name        exp_product1         exp_product2          exp_product1
1          John        2013-03-01           2013-02-28            2013-03-27
2          Alice       2013-02-25           2013-04-25            2013-10-10
3          Ken         2013-01-10           2013-01-20            2013-02-11
4          Elise       2013-04-11           2013-05-23            2013-11-11
5          Bruce       2013-03-14           2013-06-06            2013-09-10

From the table above. There’re 4 products from several users are going to be expired withing the next 30 days.

  1. John = exp_product1, exp_product2
  2. Alice = exp_product1 and
  3. Bruce = exp_product1

So I wrote:

    SELECT count(*) AS exp_pax1 
FROM   user_db 
WHERE exp_product1<=timestampadd(day, 30, now())
       AND exp_product1 >= now();

SELECT count(*) AS exp_pax2 
FROM   user_db 
WHERE exp_product2<=timestampadd(day, 30, now())
       AND exp_product2 >= now();

SELECT count(*) AS exp_pax3 
FROM   user_db 
WHERE exp_product3<=timestampadd(day, 30, now())
       AND exp_product3 >= now();

Then I sum those results with PHP: (assuming the values were fetched)

$exp_pax1=exp_pax1
$exp_pax2=exp_pax2
$exp_pax3=exp_pax3

$total_exp=$exp_pax1+$exp_pax2+$exp_pax3;//4

Well, the very question is: How could I shorten these lines into a single mySQL command? Please suggest.

Regards,

  • 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-19T02:20:50+00:00Added an answer on June 19, 2026 at 2:20 am

    You can replace count(*) with sum(YOUR CONDITION) to get count of matching rows…
    So, your query may be changed to

    SELECT sum(exp_product1<=timestampadd(day, 30, now()) AND exp_product1 >= now()) AS exp_pax1 
     FROM   user_db 
    

    Now you can add product 2 and 3 and get total

    SELECT sum(exp_product1<=timestampadd(day, 30, now()) AND exp_product1 >= now()) + 
        sum(exp_product2<=timestampadd(day, 30, now()) AND exp_product2 >= now()) +
        sum(exp_product3<=timestampadd(day, 30, now()) AND exp_product3 >= now())
        AS exp 
    FROM   user_db 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem is that i do all according this topic http://www.yiiframework.com/doc/guide/1.1/en/topics.error , but when in
this question is according to this topic: creating a huge dummy file in a
According to this post: https://groups.google.com/forum/?fromgroups#!topic/kivy-users/n7c3thksnzg , it is possible to use Eclipse as an
I'm new in the world of Node.js According to this topic: What is Node.js'
According to this topic: https://stackoverflow.com/questions/14494922/examples-of-websocket-usage-in-haskell I have my first question. Why official example of
I know that this is a popular topic, but I've yet to find an
I found a lot of threads more or less related to this topic and
According this article , generic JPA DAO(Data Access Object) is a pretty nice pattern.
I didnt find anything according this issue. Can jaas be used to secure my
I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>

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.