SQL - 拍卖 - 列出多个最高出价
SQL - Auctions - List multiple highest bids
我的拍卖
CREATE TABLE Auction (
id SERIAL PRIMARY KEY,
owner_id INTEGER REFERENCES Registered(user_id),
title VARCHAR(50) NOT NULL,
starting_bid NUMERIC(10,2) NOT NULL CHECK (starting_bid >= 0),
starting_time TIMESTAMP NOT NULL,
ending_time TIMESTAMP NULL CHECK (starting_time < ending_time),
status_id INTEGER REFERENCES AuctionStatus(id),
winner_id INTEGER REFERENCES Registered(user_id) NULL,
highest_bid NUMERIC(10,2) NOT NULL
);
我的出价
CREATE TABLE Bid (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES Registered(user_id),
auction_id INTEGER REFERENCES Auction(id),
bid_value NUMERIC(10,2) CHECK (VALUE > 0),
creation_date TIMESTAMP NOT NULL,
valid BOOLEAN DEFAULT TRUE NOT NULL
);
我希望用户能够列出他出价的所有拍卖 + 他在每次拍卖中的最高出价。
我从
开始
SELECT auction.id, auction.title, auction.highest_bid, bid.bid_value
FROM auction
JOIN bid
ON auction.id = bid.auction_id
WHERE bid.owner_id = :userID AND bid.active = TRUE AND auction.status_id = 1
列出他的所有出价,但我无法从这里继续前进以获取用户对每次拍卖的最高出价。
SELECT distinct on (auction.id) auction.id, auction.title, auction.highest_bid, bid.bid_value
FROM auction
JOIN bid
ON auction.id = bid.auction_id
WHERE bid.owner_id = :userID AND bid.active = TRUE AND auction.status_id = 1
ORDER BY auction.id, bid.bid_value desc;
有关详细信息,请参阅:
Select first row in each GROUP BY group?
和
"DISTINCT Clause" 段落来自
http://www.postgresql.org/docs/current/static/sql-select.html
我的拍卖
CREATE TABLE Auction (
id SERIAL PRIMARY KEY,
owner_id INTEGER REFERENCES Registered(user_id),
title VARCHAR(50) NOT NULL,
starting_bid NUMERIC(10,2) NOT NULL CHECK (starting_bid >= 0),
starting_time TIMESTAMP NOT NULL,
ending_time TIMESTAMP NULL CHECK (starting_time < ending_time),
status_id INTEGER REFERENCES AuctionStatus(id),
winner_id INTEGER REFERENCES Registered(user_id) NULL,
highest_bid NUMERIC(10,2) NOT NULL
);
我的出价
CREATE TABLE Bid (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES Registered(user_id),
auction_id INTEGER REFERENCES Auction(id),
bid_value NUMERIC(10,2) CHECK (VALUE > 0),
creation_date TIMESTAMP NOT NULL,
valid BOOLEAN DEFAULT TRUE NOT NULL
);
我希望用户能够列出他出价的所有拍卖 + 他在每次拍卖中的最高出价。
我从
开始SELECT auction.id, auction.title, auction.highest_bid, bid.bid_value
FROM auction
JOIN bid
ON auction.id = bid.auction_id
WHERE bid.owner_id = :userID AND bid.active = TRUE AND auction.status_id = 1
列出他的所有出价,但我无法从这里继续前进以获取用户对每次拍卖的最高出价。
SELECT distinct on (auction.id) auction.id, auction.title, auction.highest_bid, bid.bid_value
FROM auction
JOIN bid
ON auction.id = bid.auction_id
WHERE bid.owner_id = :userID AND bid.active = TRUE AND auction.status_id = 1
ORDER BY auction.id, bid.bid_value desc;
有关详细信息,请参阅:
Select first row in each GROUP BY group?
和
"DISTINCT Clause" 段落来自 http://www.postgresql.org/docs/current/static/sql-select.html