Jump to content

getting out values from multiple values


aram
 Share

Recommended Posts

Hi, i have a table that contains some values like shown down, and i want to get out some values that the summarry of theme equals to another value let's say like 13: id-------values------------------1-----------42-----------73-----------54-----------6------------------ i want to get out those values that equals 13. some help please

Edited by aram
Link to comment
Share on other sites

"Summary"? You mean "sum"?Perhaps:

SELECT * FROM table WHERE id+values=13

Of the numbers in the table you show, this will not match anything, as1+4=52+7=93+5=84+6=10

Link to comment
Share on other sites

"Summary"? You mean "sum"? Perhaps:
SELECT * FROM table WHERE id+values=13

Of the numbers in the table you show, this will not match anything, as1+4=52+7=93+5=84+6=10

yes i want sumbut i want the value only from values field. like 7+6=13 Edited by aram
Link to comment
Share on other sites

Ah, I see... try this:

SELECT t1.* FROM table AS t1INNER JOIN table AS t2WHERE t1.id != t2.id AND t1.`values` + t2.`values` = 13

("values" is a keyword in SQL, so it needs to be quoted with the "`" sign)The first two lines generate all permutations of values, the first conditions ensures you're not comparing a row to itself (otherwise, a single column of 6.5 will create a match), and the second one is the check you're interested in. The output will contain each value on its own row, with its ID, but you can be sure all values returned are part of a sum with another returned number.Note that to be matched, the number must produce the sum with exactly one other number.

  • Like 1
Link to comment
Share on other sites

Ah, I see... try this:
SELECT t1.* FROM tables AS t1INNER JOIN tables AS t2WHERE t1.id != t2.id AND t1.`values` + t2.`values` = 13

("values" is a keyword in SQL, so it needs to be quoted with the "`" sign) The first two lines generate all permutations of values, the first conditions ensures you're not comparing a row to itself (otherwise, a single column of 6.5 will create a match), and the second one is the check you're interested in. The output will contain each value on its own row, with its ID, but you can be sure all values returned are part of a sum with another returned number. Note that to be matched, the number must produce the sum with exactly one other number.

wow that's great, but let's say i want more than two values or even more like4+7+5=16 or 4+7+5+6=22what i mean here i dont want only two numbers, some times i need more, it depends on the value that i want
Link to comment
Share on other sites

Besides using PHP to generate additional joins as part of the query, as well as all the conditions in the WHERE, I can't think of a way, though I'm sure there is one.

Link to comment
Share on other sites

This is far from a trivial task to do with SQL alone... you may have a better chance at a more specialized forum... maybe the MySQL forums...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...