[英]How can I make a condition inside a query using MySQL
I am building an auction system. 我正在建立一个拍卖系统。 And I am in the process that will display every bid transaction that a user has.
我正在显示用户进行的每笔投标交易的过程。 In my table view I want to show if a certain bid transaction is the highest bid and I want to identify also if there is a higher bid than the user's bid.
在我的表格视图中,我想显示某个出价交易是否是最高出价,并且我还要确定出价是否高于用户的出价。
Here's my query: 这是我的查询:
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;
In this query I can get the last bid of a certain user. 在此查询中,我可以获得特定用户的最后出价。
+--------+------------+---------------------+---------------+
| 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 |
+--------+------------+---------------------+---------------+
I want to add another column beside bidding price that will identify if there's a much more higher bid. 我想在出价旁边添加另一列,以识别是否有更高的出价。
If the user has the highest bid I want to add a status like 'First Place' and if there is a much more higher bid it will display 'Bid Again' 如果用户的出价最高,我想添加“第一名”之类的状态 ,如果出价更高,它将显示“再次出价”
Is it possible in a query? 查询中可能吗? I read about control flow but I don't know if I can use this.
我读到有关控制流的信息,但我不知道是否可以使用它。 If not possible maybe I will do this on the PHP side.
如果不可能的话,也许我会在PHP方面执行此操作。
Thats all I hope you can help me. 以上就是我希望您能为我提供的帮助。
User's bids: 用户的出价:
select *
from auction_product_bidding
where user_id = 1
Current auctions: 当前拍卖:
select *
from auction_product
where datetime_end > now()
and status = 0
Highest bids: 最高出价:
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
)
The three combined: 三者结合:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.