2011-08-30 52 views
1

说我有哪些返回以下结果查询:MySQL:根据条件对不同行执行操作?

| val | type | i | 
---------------------- 
| 59 | 1 | 1 | 
| 40 | 2 | 2 | 
| 12 | 1 | 3 | 
| 3  | 2 | 4 | 
| 24 | 1 | 5 | 
| 30 | 1 | 6 | 
| 98 | 2 | 7 | 
| 45 | 2 | 8 | 
| 46 | 1 | 9 | 

val = an arbitrary number 
type = 1 or 2 
i = auto increment column, just for reference 

我想执行以下减法:

A - B 

A: Value of row of type '2' 
B: Value of row above A of type '1' if it exists, but without crossing a row of type '2' 

我真的希望是有道理的..

一些例子:

Row i=2 is of type 2. So the first row above it of type 1 is i=1. 
So the subtraction will be: 40 - 59 = -19 

Row i=7 is of type 2. So the first row above it of type 1 is i=6 
So the subtraction will be: 98 - 30 = 68 

Row i=8 is of type 2. But if we go above we cross a value of type 2. 
So the subtraction should return 0, or be ignored, whichever is simplest. 

最后结果t应该返回type = 1的值的列和减去的值。

如:

| val | diff | 
-------------- 
| 59 | -19 | 
| 12 | -9 | 
| 24 | 0 | *** 
| 30 | 68 | 
| 46 | 0 | *** 

*** Return diff=0 if the subtraction was never made for this row. 

我有得到我最初的表的查询。我知道如何做减法(在真实的情况下,val是一个日期,但我为这个例子保持简单)。

我不知道该怎么做的唯一的事情是在MySQL中根据不同的行执行逻辑。例如,如何根据某些条件找到最近的行,然后引用该行对其执行某些操作?有没有可能的方式来引用当前的'我',并做一个subrtaction(行i)minus(行i-1)或类似的东西。

我已经花了很多时间在这个上,我只是要放弃,并在PHP中做,而不是性能打击。作为最后一招,我在这里问是否有办法直接在mysql中执行所有这些操作?

我不需要一个完整的解决方案,因为这是非常复杂的要求,但我会采取任何形式的建议或指针,你可以给我。我不是MySQL最好的,所以也许我错过了一些简单的东西。

谢谢。

回答

2
SELECT t1.val AS val, IFNULL(t2.val, t1.val) - t1.val AS diff FROM my_table AS t1 
LEFT JOIN my_table AS t2 ON(t2.type = 2 AND t2.i = t1.i + 1) 
WHERE t1.type = 1 ORDER BY t1.i; 

测试您的数据,结果是:

| val | diff | 
-------------- 
| 59 | -19 | 
| 12 | -9 | 
| 24 | 0 | 
| 30 | 68 | 
| 46 | 0 | 
+0

当没有人帮助你时,它总是更好:)。尽管如此,谢谢你,这是完美的。现在我只需要研究它直到对我有意义。 – nebs

0

你需要的是一个存储过程,在你的表上有一个cursor。这应该是一个很好的起点。

CREATE PROCEDURE curdemo() 
BEGIN 
    DECLARE done INT DEFAULT 0; 
    DECLARE val, type, prev_val, prev_type INT; 
    DECLARE cur1 CURSOR FOR SELECT val, type, i from your_table; 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; 

    OPEN cur1; 

    read_loop: LOOP 
    FETCH cur1 INTO val, tpye, i; 
    IF done THEN 
     LEAVE read_loop; 
    END IF; 

    -- your logic goes here, sorry, not sure I understood correctly, but roughly here it goes 
    IF type = 2 THEN 
     IF prev_type is not null and prev_type = 1 THEN 
     INSERT INTO result_table VALUES (prev_val, val - prev_val); 
     END IF; 
    END IF; 
    SET prev_val = val; 
    SET prev_type = type; 
    END LOOP; 

    CLOSE cur1; 
END; 
+0

谢谢你的提示,将检查了这一点。 – nebs

0

也许你可以做这样的事情:

select t1.val, (t1.val - t2.val) as diff 
    from table as t1, table as t2 
    where t1.i = t2.i + 1 
     and t1.type = 1 
     and t2.type = 2 

用零来选择,你可以做这样一个黑客:

select t1.val, (t1.val - t2.val)*(t2.type - t1.type) as diff 
    from table as t1, table as t2 
    where t1.i = t2.i + 1 
     and t1.type = 1 

得到最后为零时,有一个,加入的:

select val, 0 as diff from 
    select val 
     from table 
     order by i desc 
     limit 1