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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:08:54+00:00 2026-06-14T00:08:54+00:00

There is a table reservations in mysql database in the below format. id |

  • 0

There is a table reservations in mysql database in the below format.

id | customer_id | user_id | date_booked
1  | 1           | 1       | 2012-11-5

I want to get the count of customers for a particular User. so I wrote query

select count(*), user_id, customer_id,date_booked from reservations where user_id=1 group by customer_id

This fetches results in desired way. But date_booked , I get the first value recorded. i.e., If there are 4 records for a customer with id 2 , It fetches 1st record’s date_booked value. I want the latest value , So I improved the query like below

select count(*), user_id, customer_id,max(date_booked) from reservations where user_id=1 group by customer_id

This fetches highest date recorded. This is still not the result set I want to fetch. I want the latest date but that should be less that current date (today).

Is there a way to write condition like this

max(date_booked)<date(now())

I want to fetch max date but less than today. I want to achieve this in CakePHP. If someone can help with Query I can write it cakePHP format.

Please improve the question if necessary.

  • 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-14T00:08:55+00:00Added an answer on June 14, 2026 at 12:08 am
    select  count(*),a.user_id,a.customer_id,
       IF(max(a.date_booked)>now(), b.date_booked, max(a.date_booked)) as date_booked
    from reservations a left join 
       (select max(date_booked) as date_booked,customer_id 
            from reservations where date_booked < now() group by customer_id) b
    on a.customer_id=b.customer_id
    where a.user_id=1 
    group by customer_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a MySQL (5.1) database table there is data that represents: how long a
I have a table in MySQL database. In that table there is a text
I have several data s in MySQL database. In my table there is a
i have a t_class table in mySql, in this table there are 3 columns,
There is a table: create table table1 ( id integer primary key, user_id varchar(36),
I have a list of reservations in a MySQL db, and I want to
I have there mysql table: **product (id,name)** 1 Samsung 2 Toshiba 3 Sony **attribute
There are table A and table B. I want to join these tables on
ive got 1 database table contains row: TABLE FROM reservations: attandee01 attandee02 attandee03 attandee04
Error: There are no Primary or Candidate Keys in the referenced table 'dbo.Customers' that

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.