插入新条目并同时更新整列


Inserting new entry and updating a whole column at the same time

我有一个有点复杂的问题需要解决,目前我有以下表结构(task_schedule):

id   taskid   productid   product_position
1    1        1           1
2    2        1           2
3    3        2           1
4    4        1           3
5    5        2           2
6    6        3           1

product_position的工作方式如下:它应该查看上一个最大值条目的productid和increment。

我通过以下查询(可能也更容易理解)实现了这一点:

INSERT into task_schedule (taskid, productid, product_position)
SELECT 1,1, max(product_position) + 1
FROM task_schedule
WHERE productid=1

(这些值当然是php后端的属性)

不过我现在需要一个新专栏。让我们称之为user_position,我将用一个例子重新创建我的表:

id   taskid   productid   product_position   user_position
1    1        1           1                  1
2    2        1           2                  4
3    3        2           1                  2
4    4        1           3                  6
5    5        2           2                  5
6    6        3           1                  3

好吧,这应该是如何工作的:user_position应该单独运行每个条目并查看productid,它应该基于均匀的优先级顺序构建一个职位列表。

因此,如果我按user_position订购,我会得到以下条目id列表:1 3 6 2 5 4

我需要在php中完成的一个示例:

想象一下以下列表:红-红-绿-绿-蓝-蓝-黄

$array1 = array(array("red"), array("red"), array("red"));
$array2 = array(array("green"), array("green"), array("green"), array("green"));
$array3 = array(array("blue"), array("blue"));
$array4 = array(array("yellow"));
$Arrays= array ($array1, $array2, $array3, $array4);
foreach ($Arrays as $type => $list) {
    $ArrayLength[]=count($list);
}

$MergeArray=array();
$flag=true;
for($i=0;$flag==true;$i++)
{
    $flag=false;
    for($j=0;$j < count($ArrayLength);$j++)
    {
        if( $i < $ArrayLength[$j] )
        {
           array_push( $MergeArray , $Arrays[$j][$i] );
           $flag=true;
        }
    }
}
echo "<pre>".Print_r(json_encode($MergeArray), true)."</pre>";

这给我返回了一个与我想要的模式一致的列表:

[["red"],["green"],["blue"],["yellow"],["red"],["green"],["blue"],["red"],["green"],["green"]]

以上只是我所需要的一个例子,但都是通过MySQL实现的。


不过,我现在甚至不知道如何处理这个问题,我需要一种方法来插入新条目并相应地更新user_position列。

如果我在表中添加一个新条目,并使用一个新的peoduct_id,这应该是最终结果:

id   taskid   productid   product_position   user_position
1    1        1           1                  1
2    2        1           2                  5
3    3        2           1                  2
4    4        1           3                  7
5    5        2           2                  6
6    6        3           1                  3
7    7        4           1                  4

请注意user_position是如何移动以适应新行的。

我认为这是一个需要解决的相当复杂的问题(或者可能我的mysql太弱了),我不认为有一个简单的解决方案,所以你可能对如何通过mysql来解决这个问题有任何意见(mysql做繁重的工作会很好),但我也欢迎一个带有多个查询之类的PHP解决方案。不幸的是,我不能删除这一列,抓取条目并让PHP对信息进行排序很容易,但我确实需要在数据库中实现这一点。

社区的任何投入都将是非常棒的。

使用的普通插入查询插入

  INSERT into task_schedule (taskid, productid, product_position, user_position)
  SELECT 1,1, max(product_position) + 1, 0 FROM task_schedule WHERE productid=1;

然后使用以下基于product_position更新所有条目

  SET @counter = 0;
  UPDATE task_schedule SET user_position = @counter := @counter + 1 ORDER BY product_position asc, productid asc;

这是SQL fiddle链接