Need some help with mysql.

需要一些mysql的帮助。

I have a table full of bids, i need to check if there are more than 3 bids from the same user that are one after another.

我有一个完整的出价表,我需要检查来自同一个用户的出价是否超过3个。

Here is some code for example:

以下是一些代码示例:

$all = sql_get('select all bids from bids where auction_id = 1 order by amount asc');
$total = 0;

foreach ($all as $bid) {
  if ($bid->user_id == $user->id) {
    if (++$total <= 3) continue;
    $bid->burned = true;
    sql_store($bid);
    show_error('you cant have more than 4 bids one after another, the last bid was burned.');
  } else {
    $total = 0;
  }
}

Is there a way to do that in a single query ?

有没有办法在单个查询中执行此操作?

2 个解决方案

#1


1

SELECT user_id, CASE WHEN @user_id = user_id 
    THEN @rownum := @rownum + 1 
    ELSE ((@user_id := user_id) * 0) + (@rownum := 1) END AS rownum
FROM (  SELECT user_id
        FROM bids, (SELECT @rownum := 0, @user_id := NULL) AS vars
        WHERE auction_id = 1
        ORDER BY created DESC) AS h
HAVING user_id = '{$user->id}' AND rownum >= 3

Simply parse user_id into query and you will know if there are 3 in a row. This asumes that you save the time the rows were created with the created column.

只需将user_id解析为查询,您就会知道连续是否有3个。这假设您使用创建的列保存创建行的时间。

更多相关文章

  1. sqlmap遇到url重写的示例
  2. AppScan安全问题解决方案
  3. PLSQL乱码解决方案
  4. 数据库不支持中文解决方案(mysql)
  5. Mysql替代解决方案Cassandra
  6. [置顶] Android屏幕适配解决方案
  7. 物流货运移动APP解决方案
  8. 【java开发系列】—— struts2简单入门示例
  9. 转:Android Studio Error:Connection timed out: connect.解决方

随机推荐

  1. Android重量级开发之--提高android启动速
  2. Android之常见问题集锦
  3. Android Studio安装教程
  4. android studio 获取sha1 和 md5 的值
  5. 【Android(安卓)Studio初探】快捷键大全
  6. Android中AsyncTask的使用
  7. XML布局
  8. Android 安全加密:非对称加密详解
  9. Android内存管理基本介绍
  10. 从android角度看2D加速图形引擎