事件调度程序计算结果的 if 条件

event scheduler count in if condition for a result

我有以下查询,如果计数是唯一的,即 1,我想更新数据库。我已经在事件调度程序 mysql 中创建了所有这些事件。但是有些问题我无法确定。如果可能请帮助我,查询如下:

SELECT user_id, count(*) as cnt from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice

投标价格应该是唯一的,以便在事件调度器

中更新TABLE

我想在事件调度程序中执行此操作。我在事件调度程序中所做的思考如下:

DELIMITER |
DO
BEGIN 
DECLARE cnt AS INT;
SELECT user_id, count(*) as cnt from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice DESC LIMIT 1;    
IF cnt=1 THEN

SET @userid = SELECT user_id from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice DESC LIMIT 1;    
update products p join auction_details a on a.product_id = p.product_id join users u on u.user_id = a.user_id set p.winner = @userid WHERE p.end_time ='2018-09-12 08:35:30';
END IF;    

结束 | 定界符 ;

预期输出应该从具有唯一计数(即 1)的查询中获取用户 ID,并更新产品 table 并将获胜者设置为该用户 ID。

你需要 ON 条款而不是 WHERE,还有 GROUP BY user_id

SELECT user_id, count(*) as cnt 
from auction_details a 
JOIN products p 
  ON p.product_id = a.product_id 
  AND p.end_time ='2018-09-12 08:35:30' 
GROUP by user_id
order by user_id