我有一张桌子
ID DID DValue
1 100 120
1 200 100
2 100 800
2 200 200
另一张表是
ID AValue
1 100
2 200
我希望将Dvalue
和Avalue
相乘,然后除以1000.同一个ID应该有2个值,因为一个DID
有id
个{ {1}}。
O / P表
id value
1 value of did 100 * avalue of id 1/1000
1 value of did 200 * avalue of id 1 /1000
2 value of did 100 * avalue of id 2/1000
2 value of did 200 * avalue of id 2 /1000
答案 0 :(得分:1)
只有INNER JOIN
两个表格。
SELECT t1.ID, ((t1.Dvalue * t2.Avalue) / 1000) AS Value
FROM yourtable1 t1
INNER JOIN yourtable2 t2 ON t1.Id = t2.Id
表脚本
CREATE TABLE yourtable1
(`ID` int, `DID` int, `DValue` int)
;
INSERT INTO yourtable1
(`ID`, `DID`, `DValue`)
VALUES
(1, 100, 120),
(1, 200, 100),
(2, 100, 800),
(2, 200, 200)
;
CREATE TABLE yourtable2
(`ID` int, `AValue` int)
;
INSERT INTO yourtable2
(`ID`, `AValue`)
VALUES
(1, 100),
(2, 200)
;
输出
ID Value
1 12
1 10
2 160
2 40
答案 1 :(得分:0)
试试这个
SELECT t1.ID, t1.Dvalue*t2.Avalue/1000 from table1 t1 inner join
table2 t2 on t1.Id=t2.Id