2016-08-16 5 views
0

私はオークションシステムを構築しています。そして私は、ユーザーが持っているすべての入札取引を表示するプロセスに入っています。私のテーブルビューでは、特定の入札取引が最高入札価格であるかどうかを示したいと思っています。ユーザーの入札価格よりも高い入札価格があるかどうかを確認したいと思います。ここでMySQLを使用してクエリ内で条件を作成する方法

は私のクエリです:このクエリで

SELECT 
pb.id AS bid_id, 
pb.product_id AS product_id, 
pb.bidding_date AS bidding_date, 
MAX(bidding_price) AS bidding_price 
FROM auction_product_bidding AS pb 
LEFT JOIN auction_product AS p 
ON(pb.product_id = p.id) 
WHERE pb.user_id = 1 
AND p.datetime_end > NOW() 
AND p.`status` = 0 
GROUP BY pb.product_id; 

私は特定のユーザーの最後の入札を得ることができます。

+--------+------------+---------------------+---------------+ 
| bid_id | product_id | bidding_date  | bidding_price | 
+--------+------------+---------------------+---------------+ 
|  55 |   4 | 2016-08-01 11:50:51 |  118000.00 | 
|  74 |   13 | 2016-08-11 14:14:25 |  202.00 | 
+--------+------------+---------------------+---------------+ 

さらに高い入札があるかどうかを識別する入札価格の横に、別の列を追加したいと考えています。

ユーザーは、私が「ファースト・プレイス」よう状態を追加したい落札金額を持っており、それが表示されますはるかに高い入札があった場合に「入札再び」

はそれをされた場合クエリで可能ですか?私は制御の流れについて読んだが、私はこれを使うことができるかどうかわからない。可能であれば、私はPHP側でこれを行います。

私はあなたが私を助けてくれることを願っています。

+1

あなたの質問は間違っているか、意味がありません。ユーザーはオークション(product_id)ごとに1回しか入札できませんが、bidding_priceに「MAX」は必要ないか、ユーザーが複数の入札単価を設定できますが、ユーザーの入札単価(bid_id、 'bidding_date')を任意に選択します。 –

+0

ところでここで左の結合を使っているのはなぜですか?それは意味をなさない。そして、where句の終了時刻とステータスを比較すると、これは単なる内部結合となります(これらの条件は外部結合された行をすべて破棄するためです)。 –

答えて

2

ユーザーズ入札:

select * 
from auction_product_bidding 
where user_id = 1 

現在のオークション:

select * 
from auction_product 
where datetime_end > now() 
and status = 0 

最高入札:組み合わせ

select * 
from auction_product_bidding 
where (product_id, bidding_price) in 
(
    select product_id, max(bidding_price) as max_price 
    from auction_product_bidding 
    group by product_id 
) 

3:

select 
    p.product_name, 
    usrpb.id as bid_id, 
    usrpb.product_id as product_id, 
    usrpb.bidding_date as user_bidding_date, 
    usrpb.bidding_price as user_bidding_price, 
    max(usrpb.bidding_price) as user_bidding_price, 
    maxpb.bidding_price as max_bidding_price, 
    maxpb.bidding_price as max_bidding_price, 
    case when usrpb.user_id = maxpb.user_id then 'yes' else 'no' end as user_is_max_bidder 
from auction_product_bidding usrpb 
join auction_product p on p.id = usrpb.product_id 
join 
(
    select * 
    from auction_product_bidding 
    where (product_id, bidding_price) in 
    (
    select product_id, max(bidding_price) as bidding_price 
    from auction_product_bidding 
    group by product_id 
) 
) maxpb on maxpb.product_id = usrpb.product_id 
where usrpb.user_id = 1 
and p.datetime_end > now() 
and p.status = 0; 
+0

助けてくれてありがとう。しかし、エラーがあります。私が最高の2つの入札アイテムを持っているときは、1つのレコードしか表示されません。 :( – Jerielle

関連する問題