我有以下查询我想更新数据库,如果计数是唯一的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;
结束| DELIMITER;
期望的输出应该从查询中获得具有唯一计数的用户id,即1并更新products表并将获胜者设置为该用户id。
你需要ON
clausule而不是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