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

标签 php mysql events triggers scheduler

我有以下查询,如果计数是唯一的,即 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

enter image description here

出价应该是唯一的,以便更新事件调度程序中的表

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

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。

enter image description here

最佳答案

您需要 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/

相关文章:

PHP:MySQL 变量值在数组中没有改变?

.net - NNTP usenet 下载消息

Mysql:如何用同一表中另一行的值填充一行中的空值?

C# 从控件列表引发事件

c# - 每次当前事件窗口发生变化时,如何让我的 C# 程序执行某些操作?

php - 如何在我的目录图片中包含一个链接?

php - 当值为日期变量时如何更新 mysql 数据库中的列

php - php 中的 DELETE sql 语句不起作用

javascript - e.preventDefault() 可以逆转吗?

php - 创建市场的起点