Jump to content

mysql help


blankextacy

Recommended Posts

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
Share on other sites

[quote author=blankextacy link=topic=122548.msg505551#msg505551 date=1168899255]
ok say the list is
1. 1000
2. 500
3. 300
i 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 TABLE
table2
(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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.