简体   繁体   English

如何在MySQL中找到具有多行总和的ID?

[英]How to find ids with sum of multiple rows in mysql?

Here is my mysql table. 这是我的mysql表。

CREATE TABLE IF NOT EXISTS  tbl_money  (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
   amount  int(11) NOT NULL,
   used  int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

INSERT INTO  tbl_money  ( id ,  amount ,  used ) VALUES
(8, 2, 0),
(9, 4, 0),
(10, 4, 0),
(11, 3, 0),
(12, 8, 0),
(13, 10, 0),
(14, 13, 0);

Id is a primary key field and money can have any number from 1 to any number. id是主键字段,money可以具有从1到任意数字的任意数字。

Problem: Suppose i have to find id from the table where amount is 8. It is simple because id 5 contains amount 8. But if i have to find id with 14 amount which does not exist. 问题:假设我必须从数量为8的表中找到ID,这很简单,因为ID 5包含数量8。但是如果我必须找到ID与14的数量不存在。 I have to select 2 or more fields now which sum will be equal to 14. For example in above table i can select id 6 and id 3 which amount sum is equal to 14. Same as if i need to find 6 i have to select id 1 and 2 because their amount sum is equal to six. 我现在必须选择2个或更多字段,它们的总和等于14。例如,在上表中,我可以选择ID 6和ID 3,其总和等于14。就像我需要查找6一样,我必须选择id 1和2,因为它们的总和等于6。

Sometime there we may need to select more than 2 rows to make equal sum. 有时,我们可能需要选择两行以上才能得出相等的总和。 But if any condition does't match with all rows of the table, we can return 0. 但是,如果任何条件与表的所有行都不匹配,我们可以返回0。

Condition: The row which is already taken that cannot be selected again. 条件:已被选择的行不能再次选择。 Used field should always be 0. If used fields value is 1 we cannot select it. “使用的字段”应始终为0。如果“使用的字段”值为1,则无法选择它。 We will only search for even number so we don't need to worry with odd numbers. 我们只会搜索偶数,因此我们无需担心奇数。

Please suggest me how to solve this algorithm. 请建议我如何解决此算法。 Thanks in advance. 提前致谢。

Below is just an idea, try implementing it in function or SP. 下面只是一个想法,请尝试在函数或SP中实现它。

Idea 1: 想法1:

i. 一世。 Find whether the value which you got is ODD or EVEN . 查找您获得的值是ODD还是EVEN

ii. ii。 If even, then try adding two EVEN numbers or two ODD numbers in your table and check whether it matches your value. 如果是偶数,则尝试在表中添加两个偶数两个奇数 ,并检查其是否与您的值匹配。

iii. iii。 If odd, then try adding One EVEN number and One ODD number from your table and check whether it matches your value. 如果是奇数,请尝试从表中添加一个偶数一个奇数,然后检查它们是否与您的值匹配。

iv. iv。 If you did not get the result, then 如果没有得到结果,那么

  • if even, add three EVEN numbers 如果是偶数,则加上三个偶数
  • if odd, add three ODD numbers or TWO EVEN numbers and ONE ODD number 如果为奇数,则添加三个奇数两个偶数一个奇数

Idea 2: (This would be quite simple) 想法2 :(这很简单)

i. 一世。 Take first value say from your sample data, take value 2 and add with other row values like 2+4 == 14, 2+4 == 14, 2+3 == 14, 2+8 == 14, 2+10 == 14, 2+13 == 14 从样本数据中得出第一个值,然后取值2并与其他行值相加,例如2+4 == 14, 2+4 == 14, 2+3 == 14, 2+8 == 14, 2+10 == 14, 2+13 == 14

ii. ii。 Then take 2nd row value say value 4 and add with other values like 4+4 == 14, 4+3 == 14, 4+8 == 14, 4+10== 14, 4+13 == 14 . 然后取第二行值,即4并加上其他值,例如4+4 == 14, 4+3 == 14, 4+8 == 14, 4+10== 14, 4+13 == 14 Here 4+10 matches the value 14 . 这里4+10与值14相匹配。 (If you need all the combination then proceed else break with this execution). (如果需要所有组合,请继续执行,否则请中断此执行)。

iii. iii。 Once till 10+13 combination is done, then add first two values and add with other values such as 6+4, 6+3, 6+8, 6+10, 6+13 直到完成10+13组合,然后将前两个值相加并与其他值相加,例如6+4, 6+3, 6+8, 6+10, 6+13

iv. iv。 proceed until the combination 31(2+4+4+3+8+10)+13 继续直到组合31(2+4+4+3+8+10)+13

Hope this would help you a bit. 希望这会对您有所帮助。

Pure SQL way to do it, which probably isn't that efficient:- 纯粹的SQL方法可以做到,效率可能不高:

SELECT CONCAT_WS(',', a.id, b.id, c.id, d.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id
INNER JOIN tbl_money c ON c.id > b.id
INNER JOIN tbl_money d ON d.id > c.id
WHERE a.amount + b.amount + c.amount + d.amount = 14
UNION
SELECT CONCAT_WS(',', a.id, b.id, c.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id
INNER JOIN tbl_money c ON c.id > b.id
WHERE a.amount + b.amount + c.amount = 14
UNION
SELECT CONCAT_WS(',', a.id, b.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id
WHERE a.amount + b.amount = 14
UNION
SELECT a.id
FROM tbl_money a
WHERE a.amount = 14

EDIT - modified to check the used field, and also that the amounts are not odd numbers 编辑-修改以检查使用的字段,并且金额不是奇数

SELECT CONCAT_WS(',', a.id, b.id, c.id, d.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id AND b.used = 1 AND MOD(b.amount, 2) = 0
INNER JOIN tbl_money c ON c.id > b.id AND c.used = 1 AND MOD(c.amount, 2) = 0
INNER JOIN tbl_money d ON d.id > c.id AND d.used = 1 AND MOD(d.amount, 2) = 0
WHERE a.amount + b.amount + c.amount + d.amount = 14
AND a.used = 1
AND MOD(a.amount, 2) = 0
UNION
SELECT CONCAT_WS(',', a.id, b.id, c.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id AND b.used = 1 AND MOD(b.amount, 2) = 0
INNER JOIN tbl_money c ON c.id > b.id AND c.used = 1 AND MOD(c.amount, 2) = 0
WHERE a.amount + b.amount + c.amount = 14
AND a.used = 1
AND MOD(a.amount, 2) = 0
UNION
SELECT CONCAT_WS(',', a.id, b.id)
FROM tbl_money a
INNER JOIN tbl_money b ON b.id > a.id AND b.used = 1 AND MOD(b.amount, 2) = 0
WHERE a.amount + b.amount = 14
AND a.used = 1
AND MOD(a.amount, 2) = 0
UNION
SELECT a.id
FROM tbl_money a
WHERE a.amount = 14
AND a.used = 1
AND MOD(a.amount, 2) = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM