我有你下面的查询,我想更新数据库,如果计数来唯一的是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
投标价格应该是唯一的,以便在事件调度器中更新表
我想在事件调度程序中执行此操作。我在事件调度器中所做的工作如下:
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,并更新products表并将胜利者设置为该用户id。
1条答案
按热度按时间bzzcjhmw1#
你需要
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