2011-11-22 78 views
-2

您好我正在尝试创建一个SQL查询是我将其行下的行中列的值乘以1行的值。我该怎么做?乘两列很容易,但我似乎卡在乘以2行。在sql中相乘2行

+1

可能重复[SQL Server查询带来的GroupWise倍增](http://stackoverflow.com/questions/3653586/sql-server -query-to-bring-groupwise-multiplication) – gbn

+1

向我们展示你的表格defs – Marco

+0

这将有助于:http://stackoverflow.com/questions/8185873/multiply-rows-in-t-sql – AlphaMale

回答

3

这取决于你的情况。

要simplyfy它......如果你的表是这样的......

CREATE TABLE `test` (
    `number` BIGINT(20) NOT NULL, 
    PRIMARY KEY (`number`) 
) ENGINE=INNODB DEFAULT CHARSET=latin1 

...在像这样的一些数据...

number 
------ 
    2 
    3 
    4 

...你可以做这样的事情:

SELECT 
    (SELECT number 
    FROM test 
    WHERE number = 2) * 
    (SELECT number 
    FROM test 
    WHERE number = 3) AS "product" 
FROM test 
LIMIT 1; 

你可能只是藏汉在JOIN表到自身。

1

加入对同桌

select t1.val*t2.val from tab t1, tab t2 where t1.id=5 and t2.id=6 

select t1.val*t2.val from tab t1, tab t2 where t1.id=5 and t2.id=t1.id+1