简体   繁体   中英

event scheduler count in if condition for a result

I have thee following query i want to update the database if the count comes unique that is 1. I have created all this event in event scheduler mysql. But there are problems which i cannot identify. Please help me if possible, the query as below:

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

在此处输入图片说明

THE BIDDING PRICE SHOULD BE UNIQUE TO UPDATE THE TABLE IN EVENT SCHEDULER

I want to occur this in the event scheduler. The think i have done in the event scheduler is as follow:

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;    

END | DELIMITER ;

Expected output is supposed to get the user id from the query which have the unique count ie 1 and update the products table and set the winner to be that user id.

在此处输入图片说明

You need ON clausule instead of WHERE , also 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM