从SQL中的单个表中获取相同属性的两个不同值

时间:2013-04-14 13:20:33

标签: asp.net mysql sql

我有三张桌子即。出价(bid_id,base_price),客户(customer_id,name,..)和Customer_Bid(customer_id,bid_id,bidding_amount),其中客户出价及其出价金额存储在Customer_Bid表中。 我想显示客户的详细信息以及他的出价ID以及出价相同的出价ID。

我已尝试获取客户详细信息,但我无法显示其出价金额以及同一表格中的最高出价金额。

Plz任何人都可以帮助我。 感谢。

修改这是评论

中的查询
select cb.bid_id, c.customer_id ,MyBid=cb.total_bidding_ammount
, HighestBid= max(cb.total_bidding_ammount) 
from customer as c
,customer_bidding as cb
,bid as b 
group by cb.bid_id, c.customer_id, cb.total_bidding_ammount 

3 个答案:

答案 0 :(得分:1)

如果你改变了这个:

, HighestBid= max(cb.total_bidding_ammount)

这样的事情:

, HighestBid = 
(select max(bidding_ammount)
from customer_bidding
where bid_id = bid.bid_id)

你将走上正轨。

答案 1 :(得分:-1)

试试这个:

SELECT  cb.bid_id, 
        c.customer_id,
        cb.total_bidding_ammount,
        topbids.customer_id as topbid_customer_id,
        topbid
FROM customer c
INNER JOIN customer_bidding as cb
ON c.customer_id = cb.customer_id
INNER JOIN (
            SELECT cb.bid_id, c.customer_id, MAX(cb.total_bidding_ammount) as topbid
            FROM customer c
            INNER JOIN customer_bidding cb
            ON (c.customer_id = cb.customer_id)
            GROUP BY cb.bid_id
           ) topbids
ON cb.bid_id = topbids.bid_id

答案 2 :(得分:-1)

select a.*, b.bidding_amount,
     e.bidding_amount as highest_bid
from customer a
inner join customer_bid b on a.customer_id = b.customer_id
inner join bids c on b.bid_id = c.bid_id
join 
    (select * from (
    select bidding_amount,bid_id
    from customer_bid
    order by bidding_amount desc
    ) d group by d.bid_id) e on b.bid_id = e.bid_id