aram Posted February 17, 2012 Share Posted February 17, 2012 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 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 17, 2012 Share Posted February 17, 2012 SELECT * FROM table WHERE values=13 Link to comment Share on other sites More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 "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 More sharing options...
aram Posted February 17, 2012 Author Share Posted February 17, 2012 SELECT * FROM table WHERE values=13thanks but it's not working Link to comment Share on other sites More sharing options...
aram Posted February 17, 2012 Author Share Posted February 17, 2012 "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 Link to comment Share on other sites More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 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. Link to comment Share on other sites More sharing options...
aram Posted February 17, 2012 Author Share Posted February 17, 2012 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 More sharing options...
boen_robot Posted February 17, 2012 Share Posted February 17, 2012 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 More sharing options...
aram Posted February 17, 2012 Author Share Posted February 17, 2012 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.Thanks Link to comment Share on other sites More sharing options...
aram Posted February 17, 2012 Author Share Posted February 17, 2012 Is there anyone else could give me a solution? Link to comment Share on other sites More sharing options...
aram Posted February 18, 2012 Author Share Posted February 18, 2012 some help please Link to comment Share on other sites More sharing options...
aram Posted February 18, 2012 Author Share Posted February 18, 2012 Can't anybody give me a solution? Link to comment Share on other sites More sharing options...
boen_robot Posted February 18, 2012 Share Posted February 18, 2012 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 More sharing options...
justsomeguy Posted February 20, 2012 Share Posted February 20, 2012 Your options are to use a stored procedure, user-defined function, or use PHP. Each situation involves dynamically creating a query with as many joins as you want to set a limit on. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.