2016-09-20 116 views
4

我有一个表,看起来像这样,MySQL的 - 基于其他值

+-----+--------+-------+ 
| num | amount | value | 
+-----+--------+-------+ 
| 1 |  12 |  1 | 
| 1 |  12 |  1 | 
| 2 |  13 |  1 | 
| 4 |  15 |  0 | 
| 2 |  13 |  1 | 
| 3 |  14 |  1 | 
| 3 |  14 |  1 | 
| 1 |  12 |  1 | 
+-----+--------+-------+ 

我想根据不同的“NUM”列总结了“量”的列,其中“价值获取所有不同行的总和'等于1, 例如,运行下面的查询后,

select DISTINCT num, amount, value from test where value =1 ; 

不同 'NUM' 基于表是

+-----+--------+-------+ 
| num | amount | value | 
+-----+--------+-------+ 
| 1 |  12 |  1 | 
| 2 |  13 |  1 | 
| 3 |  14 |  1 | 
+-----+--------+-------+ 

所以我要的最终结果是

12 + 13 + 14 = 39。

一件事是,

我不能使用子查询。

因为它已经是另一个查询的一部分了。

这里是我的表的脚本是

SET FOREIGN_KEY_CHECKS=0; 

-- ---------------------------- 
-- Table structure for `test` 
-- ---------------------------- 
DROP TABLE IF EXISTS `test`; 
CREATE TABLE `test` (
    `num` int(11) DEFAULT NULL, 
    `amount` int(11) DEFAULT NULL, 
    `value` int(11) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of test 
-- ---------------------------- 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('4', '15', '0'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
+1

只使用和'选择总和(金额)从测试下value = 1;' –

+0

你期待只有1行INT结果组? –

+0

@P.Salmon完全是1行结果。最后的答案是39 –

回答

0
select num,sum(amount)/Count(*) 
from test 
where value = 1 
group by num 
order by num 
+0

没有帮助:( –

0

我认为每一个NUM具有相同的值(如果没有,你应该如何挑选呢?)

所以,这将工作:

SELECT SUM(DISTINCT amount) 
FROM test 
WHERE value = 1 
+0

'量'无法区分。独特必须'num'列。它没有解决问题:( –

+0

为什么?我试过这个,它的工作原理。如果金额不能清晰,你要计算哪一个,假设有'num'设置为'1'的两行,以及不同'amount'的值如'12'和'13'? –

+1

@MattiaNocerino OP的意思是,金额可以是相同的不同'num'因此'DISTINCT'金额不能用 – Shaharyar

0
SELECT SUM(amount) FROM (SELECT DISTINCT num, amount, VALUE FROM test WHERE VALUE =1) AS temp ; 
+0

不能使用子查询人:( –

+0

它有帮助吗? –

0

您可以创建一个临时表包含不同的值并使用SUM()来计算金额。您的查询就会像:

SELECT SUM(amount) 
FROM (
     SELECT DISTINCT t.`num`, t.`amount` 
     FROM test t 
     WHERE t.`value`=1 
    ) temp 
+0

@AmeerHamza我已经添加了一个sol ution。检查这是否有帮助。 –

+0

他说他不能使用子查询 –

+0

正确..我不能使用子查询中:(这是限制 –

0

请检查该解决方案

SET FOREIGN_KEY_CHECKS=0; 

-- ---------------------------- 
-- Table structure for `test` 
-- ---------------------------- 
DROP TABLE IF EXISTS `test`; 
CREATE TABLE `test` (
    `num` int(11) DEFAULT NULL, 
    `amount` int(11) DEFAULT NULL, 
    `value` int(11) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of test 
-- ---------------------------- 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('4', '15', '0'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 

-- ---------------------------- 
-- Insert the result set into temp table and then use sum(). 
-- ---------------------------- 


SELECT DISTINCT `num` , `amount` 
INTO #tmp 
FROM test 
WHERE `value` =1 

SELECT * FROM #tmp 

SELECT sum(`amount`) as amount FROM #tmp