Jump to content

mysql help


blankextacy

Recommended Posts

[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
https://forums.phpfreaks.com/topic/34323-mysql-help/#findComment-161511
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.