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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:14:21+00:00 2026-06-14T17:14:21+00:00

can anyone help me solve this problem? i’ve been trying to query this but

  • 0

can anyone help me solve this problem? i’ve been trying to query this but i keep getting error.

MySQL code:

SELECT * FROM
(

 SELECT BU.*, BD.BOOK_TITLE AS BOOK_TITLE, BD.BOOK_COMPANY AS COMPANY,
     BD.RETURN_DATE AS RETURN 
 FROM BOOK_USER BU
 INNER JOIN BOOKING_DETAIL BD ON (BU.USR_ID = BD.USR_ID)
                
UNION

 SELECT BU.*, "NEW REGISTERED" AS BOOK TITLE, 'RENT-A-BOOK' AS COMPANY,
     BU.REGISTER_DATE AS RETURN 
 FROM BOOK_USER BU

) AS BU 
GROUP BY BU.USR_ID

The Tables:

BOOK_USER

+---------+----------+---------------+
| USR_ID  | USR_NAME | REGISTER_DATE |
+---------+----------+---------------+
| 1       | john     | 2011-09-20    |
+---------+----------+--------------+
| 2       | jane     | 2011-12-05    |
+---------+----------+--------------+
| 3       | doe      | 2012-02-16    |
+---------+----------+--------------+
| 4       | mary      | 2012-02-02  |
+---------+----------+--------------+

BOOKING_DETAIL

+---------+----------+------------+-----------+--------------+
| BOOK_ID  | USR_ID |  BOOK_TITLE | COMPANY   | RETURN_DATE  |
+----------+--------+-------------+-----------+--------------+
| 1       | 1       | DEAR JOHN   |ABC PVT LMT| 2011-11-01   |
+---------+---------+-------------+-----------+--------------|
| 2       | 1       | LUCKY       |  DEF      | 2012-03-18   |
+---------+---------+-------------+-----------+--------------|
| 3       | 1       | THE RISE    | GHI       | 2012-06-12   |
+---------+---------+-------------+-----------+--------------|
| 4       | 2       | HELLO       |    TIMES  | 2012-01-11   |
+---------+---------+-------------+-----------+--------------|
| 5       | 2       | SHOPAHOLIC  |           | 2012-08-31   |
+---------+---------+-------------+-----------+--------------|
| 6       | 3       | LOST        |           | 2012-06-20   |
+---------+---------+-------------+-----------+--------------|

The result should return the latest RETURN_DATE and SORTED by USR_ID.

eg:

  1. John, THE RISE, GHI,2012-06-12
  2. Jane,SHOPAHOLIC,RENT-A-BOOK,2012-08-31
  3. doe, LOST,RENT-A-BOOK,2012-06-20
  4. mary, NEW REGISTERED,RENT-A-BOOK , 2012-02-02
  • 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-14T17:14:21+00:00Added an answer on June 14, 2026 at 5:14 pm

    The query constains a subquery which gets the latest RETURN_DATE for each user.

    SELECT  a.*, c.BOOK_TITLE, c.RETURN_DATE
    FROM    book_user a
            INNER JOIN
            (
                SELECT usr_ID, MAX(return_DATE) maxDate
                FROM booking_detail
                GROUP BY usr_ID
            ) b ON  a.usr_ID = b.usr_ID
            INNER JOIN booking_detail c
                ON  b.usr_ID = c.usr_ID AND
                    b.maxDate = c.return_DATE
    
    • SQLFiddle Demo

    UPDATE

    use LEFT JOIN and COALESCE

    SELECT  a.USR_ID,
            a.USR_NAME, 
            COALESCE(c.BOOK_TITLE,'RENT-A-BOOK') BOOK_TITLE, 
            COALESCE(c.RETURN_DATE, a.REGISTER_DATE) RETURN_DATE
    FROM    book_user a
            LEFT JOIN
            (
                SELECT usr_ID, MAX(return_DATE) maxDate
                FROM booking_detail
                GROUP BY usr_ID
            ) b ON  a.usr_ID = b.usr_ID
            LEFT JOIN booking_detail c
                ON  b.usr_ID = c.usr_ID AND
                    b.maxDate = c.return_DATE
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help solve this problem, no matter what i try i cannot display
echo <p id='xlday'>'$_POST['day']'</p>; Hi can anyone help me solve this problem, should be a
Can anyone help - this is driving me mad. I am calling a mysql
I have been trying to solve this problem the whole day: How do I
Can anyone help me to solve the following case? I am trying to generate
Can any one please help me solve this. I am resizing some flash object/embed
Can anyone help or provide me with some suggestions for the below query. I
Hi there everyone im a newbie so can anyone of u help me solve
can anyone help me, am facing some problem with the datetime function in the
I've been searching for this problem for a while now. I'm trying to update

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.