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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:46:16+00:00 2026-05-23T02:46:16+00:00

I am writing an SQL query which will return a list of auctions a

  • 0

I am writing an SQL query which will return a list of auctions a certain user is losing, like on eBay.

This is my table:

bid_id  bid_belongs_to_auction  bid_from_user   bid_price
6       7                       1               15.00
8       7                       2               19.00
13      7                       1               25.00

The problematic area is this (taken from my full query, placed at the end of the question):

      AND EXISTS (
              SELECT 1 
                FROM bids x 
               WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
                 AND x.bid_price > bids.bid_price
                 AND x.bid_from_user <> bids.bid_from_user
                 )

The problem is that the query returns all the auctions on which there are higher bids, but ignoring the user’s even higher bids.

So, an example when the above query works:

bid_id  bid_belongs_to_auction  bid_from_user   bid_price
6       7                       1               15.00
7       7                       2               18.00

In this case, user 1 is returned as losing the auction, because there is another bid higher than the users bid.

But, here is when the query doesn’t work:

bid_id  bid_belongs_to_auction  bid_from_user   bid_price
6       7                       1               15.00
8       7                       2               19.00
13      7                       1               25.00

In this case, user 1 is incorrectly returned as losing the auction, because there is another bid higher than one of his previous bids, but the user has already placed a higher bid over that.

If it’s important, here’s my full query, but I think it won’t be necessary to solve the aforementioned problem, but I’m posting it here anyway:

$query = "
   SELECT
      `bid_belongs_to_auction`,
      `auction_unixtime_expiration`,
      `auction_belongs_to_hotel`,
      `auction_seo_title`,
      `auction_title`,
      `auction_description_1`
   FROM (
      SELECT
         `bid_belongs_to_auction`,
         `bid_from_user`,
         MAX(`bid_price`) AS `bid_price`,
         `auctions`.`auction_enabled`,
         `auctions`.`auction_unixtime_expiration`,
         `auctions`.`auction_belongs_to_hotel`,
         `auctions`.`auction_seo_title`,
         `auctions`.`auction_title`,
         `auctions`.`auction_description_1`
      FROM `bids`
      LEFT JOIN `auctions` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
      WHERE `auction_enabled`='1' AND `auction_unixtime_expiration` > '$time' AND `bid_from_user`='$userId'
      AND EXISTS (
              SELECT 1 
                FROM bids x 
               WHERE x.bid_belongs_to_auction = bids.bid_belongs_to_auction
                 AND x.bid_price > bids.bid_price
                 AND x.bid_from_user <> bids.bid_from_user
                 )
      GROUP BY `bid_belongs_to_auction`
   ) AS X
   WHERE `bid_from_user`='$userId'
";
  • 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-23T02:46:17+00:00Added an answer on May 23, 2026 at 2:46 am

    Here’s a different approach:

    $query = "
       SELECT
          `max_bids`.`bid_belongs_to_auction`,
          `auctions`.`auction_unixtime_expiration`,
          `auctions`.`auction_belongs_to_hotel`,
          `auctions`.`auction_seo_title`,
          `auctions`.`auction_title`,
          `auctions`.`auction_description_1`
       FROM `auctions`
         INNER JOIN (
            SELECT
               `bid_belongs_to_auction`,
               MAX(`bid_price`) AS `auction_max_bid`,
               MAX(CASE `bid_from_user` WHEN '$userId' THEN `bid_price` END) AS `user_max_bid`
            FROM `bids`
            GROUP BY `bid_belongs_to_auction`
         ) AS `max_bids` ON `auctions`.`auction_id` = `max_bids`.`bid_belongs_to_auction`
       WHERE `auctions`.`auction_enabled`='1'
         AND `auctions`.`auction_unixtime_expiration` > '$time'
         AND `max_bids`.`user_max_bid` IS NOT NULL
         AND `max_bids`.`user_max_bid` <> `max_bids`.`auction_max_bid`
    ";
    

    Basically, when you are retrieving the max bids for all the auctions, you are also retrieving the specific user’s max bids along. Next step is to join the obtained list to the auctions table and apply an additional filter on the user’s max bid being not equal to the auction’s max bid.

    Note: the `max_bids`.`user_max_bid` IS NOT NULL condition might be unnecessary. It would definitely be so in SQL Server, because the non-nullness would be implied by the `max_bids`.`user_max_bid` <> `max_bids`.`auction_max_bid` condition. I’m not sure if it’s the same in MySQL.

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

Sidebar

Related Questions

My Table 'Levels' in a SQL Server 2005 database looks like this: LevelId Description
I would like to filter certain fields in my database which are Null, 0,
I have a table code_prices that looks something like this: CODE | DATE |
I'm writing an application that will have a SQL Server backend that will store
When I make a SQL query, for example, in database where there's a table
This answer to what looks like the same question: Convert integer to hex and
Summary I'm currently writing an application where I have located my SQL instructions into
I am writing some tool which has to retrieve column names of the retrieved
I'm writing a rails finder query that pulls out records for articles sorted by
I have a table with several columns that allow NULLs. How would I go

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.