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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:27:35+00:00 2026-05-27T05:27:35+00:00

I have a table called payment which adds all the payment entries. I have

  • 0

I have a table called payment which adds all the payment entries. I have field in payment table as customer_id which refers to the customer id in the customer table.

There is a field called flag where I store 1 or 0 based on their payment status. If they paid the full amount they flag will be 0 and if there is a balance the flag wil be 1

There is another field in payment table, added_on, which is a timestamp

I want to fetch out the latest added row of all the customer_id(in the payment table) who have flag 1(pending payment)

I tried fetching from the payment table like this

   $rResult = mysql_query("select * from payment  where flag='1'")

and then looped like this

    while ($aRow=mysql_fetch_object($rResult))
        {
        //fetch out the customer id like this
$customer= $aRow->customer_id;
    $pay_quer = mysql_query("select*from payment where customer_id='$customer'");

        } 

I know it is long to do this..is there any shorter way to do it with one query?
The results are not coming as I expected too. 🙁

  • 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-05-27T05:27:36+00:00Added an answer on May 27, 2026 at 5:27 am
    SELECT p.* 
    FROM 
        payment AS p
      JOIN
        ( SELECT customer_id
               , MAX(added_on) AS max_added_on
          FROM payment
          WHERE flag='1'
          GROUP BY customer_id
        ) AS g
        ON  g.customer_id = p.customer_id
        AND g.max_added_on = p.added_on
    ORDER BY p.added_on DESC
    

    or (it’s not clear what you want exactly nor how the flag is used). This will show different results, only those customers that their latest row in the payment has flag=1:

    SELECT p.* 
    FROM 
        payment AS p
      JOIN
        ( SELECT customer_id
               , MAX(added_on) AS max_added_on
          FROM payment
          GROUP BY customer_id
        ) AS g
        ON  g.customer_id = p.customer_id
        AND g.max_added_on = p.added_on
    WHERE p.flag='1'
    ORDER BY p.added_on DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table named payment and there is a field in it
I have table called FinalForgotten which only contains one field called aname. The field
I have a table called BlogPost which has a 1-to-many relationship with the Comment
i have a table called category in which i have main category ids and
I have table called page which represents every single page in my website. page_id
Hi I have table called Sold Products which stores buying information for product &
I have a table which has column called payment_txn_status I have to write a
i have table called type which contains data family,individual i want in some pages
I have table in my SQL Server database called OrderDetail which stores the order
Have a table called person. which has personid and contactid. if a person is

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.