blankextacy Posted January 15, 2007 Share Posted January 15, 2007 hey is it possible to select from one field the highest the second and the third highest and single them out? Quote Link to comment Share on other sites More sharing options...
shoz Posted January 15, 2007 Share Posted January 15, 2007 [code]SELECT field FROM tablename ORDER BY field DESC LIMIT 3[/code] Quote Link to comment Share on other sites More sharing options...
blankextacy Posted January 15, 2007 Author Share Posted January 15, 2007 yeah i know thati dont want to do that i want it to select the highest the insert that into the database then the 2nd and then the 3rd Quote Link to comment Share on other sites More sharing options...
shoz Posted January 15, 2007 Share Posted January 15, 2007 You'd like to insert the 3 highest numbers from one table into another table? If that is not what you'd like to do explain in greater detail what you want to accomplish. For example where are the list of numbers being stored of which the three highest numbers should be chosen etc? Quote Link to comment Share on other sites More sharing options...
blankextacy Posted January 15, 2007 Author Share Posted January 15, 2007 ok say the list is1. 10002. 5003. 300i want to add 1000 into another database as rank #1 and 500 in the same database as 1000 as a different row and then i want to add 300 in there as #3 just like i did with 1000 500. Quote Link to comment Share on other sites More sharing options...
shoz Posted January 15, 2007 Share Posted January 15, 2007 [quote author=blankextacy link=topic=122548.msg505551#msg505551 date=1168899255]ok say the list is1. 10002. 5003. 300i want to add 1000 into another database as rank #1 and 500 in the same database as 1000 as a different row and then i want to add 300 in there as #3 just like i did with 1000 500.[/quote]To create a new table with those values you should be able to use the following[code]CREATE TABLEtable2(rank TINYINT AUTO_INCREMENT PRIMARY KEY)(SELECT fieldname FROM table1 ORDER BY fieldname DESC LIMIT 3)[/code]To insert the values into an already existing table you can use something similar to the following[code]<?php$query = 'SELECT fieldname FROM table1 ORDER BY fieldname DESC LIMIT 3';$result = mysql_query($query) or die($query."<br />\n".mysql_error());if (mysql_num_rows($result)){ $values = array(); for ($i = 1; $row = mysql_fetch_assoc($result); $i++) { $values[] = "('$i', '{$row['fieldname']}')"; } $query = 'INSERT INTO table2 VALUES '.implode(',', $values); mysql_query($query) or die($query."<br />\n".mysql_error());}?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.