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? Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/ 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] Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161448 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 Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161458 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? Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161478 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. Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161488 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] Link to comment https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161511 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.