asmith Posted June 30, 2008 Share Posted June 30, 2008 hi table test id num1 num2 num3 1 23 21 34 2 34 32 3 . . . is it possible to achive this : select id from test where num1+num2+num3 is maximum Is there a query for this purpose ? Link to comment https://forums.phpfreaks.com/topic/112643-finding-max-of-columns-sum/ Share on other sites More sharing options...
br0ken Posted June 30, 2008 Share Posted June 30, 2008 Try either of these, they make work, I'm not too sure. SELECT id FROM test ORDER BY SUM(num1,num2,num3) DESC LIMIT 1 SELCET id FROM test ORDER BY (num1+num2+num3) DESC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/112643-finding-max-of-columns-sum/#findComment-578488 Share on other sites More sharing options...
asmith Posted June 30, 2008 Author Share Posted June 30, 2008 i tried the second and it works. The problem is if the sum of 2 rows is equal , that query shows only 1 row. select * from table where num1+num2 are max. --> maybe 2 rows have the max number. so i could use mysql_num_rows for it to find out . I'm looking for only one query, otherwise i can use your query, save the max sum, then again run a query ke select ..... where num1+num2+num3 = the number from previous query i'm just looking for the simplest way. Link to comment https://forums.phpfreaks.com/topic/112643-finding-max-of-columns-sum/#findComment-578503 Share on other sites More sharing options...
rhodesa Posted June 30, 2008 Share Posted June 30, 2008 only way i can think of is: SELECT * FROM test WHERE (num1 + num2 + num3) = (SELECT MAX(num1 + num2 + num3) FROM test) Link to comment https://forums.phpfreaks.com/topic/112643-finding-max-of-columns-sum/#findComment-578535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.