SQL script to find winners on auction site

Asked

Viewed 49 times

-1

I am developing an auction site in PHP and Mysql and I want to display the report of the winners - that is, of the people who made the biggest bids in different auctions (posts).

This is the structure of the entity that stores the bids is comentario(id_com, id_user, id_post, prospota). I would like to return all who have made the highest bid of each auction.

insert image description here

  • Hello @Octavio Cossa, what would be the database? Mysql? Which version? Are you using some CMS, like Wordpress? Some plugin?

1 answer

1


Assuming that no bids can be repeated for each auction, it is possible to do the following query

SELECT c1.*
FROM comentario c1
INNER JOIN (SELECT ci.id_user, MAX(ci.proposta) AS maiorproposta
            FROM comentario ci GROUP BY ci.id_post) c2
  ON (c1.proposta = c2.maiorproposta);

For this, I assumed that Mysql 5 is used with the following structure

CREATE TABLE IF NOT EXISTS `comentario` (
  `id_com` int(6) unsigned NOT NULL,
  `id_user` int(6) unsigned NOT NULL,
  `id_post` int(6) unsigned NOT NULL,
  `proposta` float NOT NULL,
  PRIMARY KEY (`id_com`)
) DEFAULT CHARSET=utf8;

I provide a SQL Fiddle with the answer in this link.

  • Thanks worked #tvdias..

  • Hello @octaviocossa, welcome to the OS in English. I advise you to follow the tour to see how best to thank for an answer :)

  • @octaviocossa, do not forget to accept the answer if it has been useful to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.