Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MySQL在查询中创建条件_Mysql_Sql - Fatal编程技术网

如何使用MySQL在查询中创建条件

如何使用MySQL在查询中创建条件,mysql,sql,Mysql,Sql,我正在建立一个拍卖系统。我正在这个过程中,它将显示用户拥有的每个出价交易。在我的表视图中,我想显示某个出价交易是否是最高出价,我还想确定是否有高于用户出价的出价 我的问题是: 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

我正在建立一个拍卖系统。我正在这个过程中,它将显示用户拥有的每个出价交易。在我的表视图中,我想显示某个出价交易是否是最高出价,我还想确定是否有高于用户出价的出价

我的问题是:

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端这样做

这就是我希望您能帮助我的全部内容。

用户出价:

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
)
三者结合起来:

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;
用户出价:

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
)
三者结合起来:

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;

您的查询不正确或没有意义。一个用户每次拍卖只能出价一次(产品id),但你不需要在出价时使用
MAX
,或者一个用户可以进行多次出价,但你只是显示用户任意选择的一个出价(
bid\u id
出价日期
)?这毫无意义。比较where子句中的结束时间和状态会使这仅仅是一个内部连接(因为这些条件会忽略任何外部连接行)。您的查询不正确或没有意义。一个用户每次拍卖只能出价一次(产品id),但你不需要在出价时使用
MAX
,或者一个用户可以进行多次出价,但你只是显示用户任意选择的一个出价(
bid\u id
出价日期
)?这毫无意义。比较where子句中的结束时间和状态会使这仅仅是一个内部连接(因为这些条件会忽略任何外部连接行)。谢谢您的帮助。但是有一个错误。当我有2个出价最高的项目时,它只显示1个记录:(谢谢你的帮助。但是有一个错误。当我有两个出价最高的项目时,它只显示一个记录。)(