我有以下查询,如果计数是唯一的,即 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,并更新产品表并将获胜者设置为该用户 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
关于php - 事件调度程序计算结果的 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52286973/