2017-07-26 71 views
0

我有一个问题;我在我的数据库中的表称为项目与像id内容,allowCommentsfromUserIdcategorycategoryTitlepricelikesCountimagesCountviewsCountreviewsCount如何重新排列SQL表:ID到最后的ID

我希望能够重新安排我的期望id是最新的id,其余的向上。

下面是一个例子:

id allowComments fromUserId category categoryTitle  price 

1  1    1   3   Phone   20000 
2  1    1   5   Car   100000 
3  1    5   2   Console  20000 
4  1    2   1   Fashion  100 
5  0    1   3   Phone   12000 
6  1    2   3   Phone   21300 

等等

所以我的问题是,我该如何使用PHP,使id3成为最后id和其他ID进行排序,在拉升有序的时尚?

就让我们说,这是我的PHP文件,从数据库

       <th>Id</th> 
           <th>category</th> 
           <th>price</th> 
           <th>cATEGORY ID </th> 
           <th>fromUserId</th> 
           <th>EDIT </th> 
           <th>REFRESH </th> 

          </tr> 

          <?php $k=1; while($get=mysql_fetch_array($insertionquery)) {?> 

<td><?php echo $get['id'];?></td> 
<td><?php echo $get['categoryTitle'];?></td> 
    <td><?php echo $get['price']; ?></td> 
    <td><?php echo $get['category']; ?></td> 
    <td><?php echo $get['fromUserId']; ?></td> 

<td><a href="editmenu.php?EDITC=<?php echo $get['id']?>"></a></td> 
<td><a href="refresh.php?REFRESH=<?php echo $get['id']?>"></a></td> 

+1

移动ID的通常被认为是一个坏主意的数据库,如何关于添加一个排序列呢? – rtfm

+0

首先将id设置为最后一个id:'UPDATE table_name SET id =(SELECT max(id)+1)FROM table_name WHERE id = 3;'。接下来重置auto_increment计数器:'ALTER TABLE table_name AUTO_INCREMENT = 1' – icecub

+0

可以请你写代码形式的php文件有点困惑 –

回答

0

好吧获取内容,下面的代码中,我评论,你需要知道的一切。阅读这些内容非常重要,以便了解它是如何工作的!我也离开你这个链接:PHP Prepared Statements,这样你就可以在更友好的环境中了解更多关于PDO和Prepared Statements的知识。在这个问题的底部,你可以找到没有所有评论的代码,以保持更清晰。

请记住,您需要在代码顶部设置正确的数据库信息。

代码的解释在评论:

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = ''; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* First we set up some information for our PDO object */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
    PDO::ATTR_PERSISTENT => true, 
    PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Next we try to instantiate the PDO object and see if we can connect to the database */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
/* Here we'll catch any database connection errors and output them so we know what's going on */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Now lets try to change row id 3 to the last row id */ 

/* First we setup our query */ 
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id'; 

/* As you can see, we don't have the number 3 in there. Instead, we have a 
    placeholder ':id'. This is because we're going to use Prepared Statements. 
    They are not needed for this case, but I want to show how to do this 
    because you do need them if you want to insert data that's provided by a user. 
    This is to prevent SQL injection. SQL injection is very easy and allows 
    visitors to your website to completely delete your database if you don't 
    protect yourself against it. This will do just that. */ 

/* So lets prepare our query first */ 
$stmt = $pdo->prepare($query); 

/* Set ID */ 
$id = 3; 

/* Now we're going to bind the data to the placeholder */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Now all that's left to do is execute it so our database gets updated */ 
try { 
    $stmt->execute(); 
} 
/* Again, catch any errors in our query and output them */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Next we need to re-arrange the id's */ 

/* First we setup our query */ 
$query = 'ALTER TABLE items AUTO_INCREMENT = 1'; 

/* Again, Prepared Statements are not nessesary here. So I'm gonna show 
    you how to do it without them. */ 

/* All we have to do is query the database directly */ 
try { 
    $pdo->query($query); 
} 
/* Again, catch any errors in our query and output them */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 

清洁代码:

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = ''; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* Set DSN and Options */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
    PDO::ATTR_PERSISTENT => true, 
    PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Instantiate the PDO object */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id'; 

/* Prepare query */ 
$stmt = $pdo->prepare($query); 

/* Set ID */ 
$id = 3; 

/* Bind values */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Execute query */ 
try { 
    $stmt->execute(); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'ALTER TABLE items AUTO_INCREMENT = 1'; 

/* Query database */ 
try { 
    $pdo->query($query); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 
+0

我得到像这样的错误致命错误:无法通过参考C:\ xampp \ htdocs \ kobobay \ admin \ refresh.php中的参数2在第33行$ stmt- > bindParam(':id',3); –

+0

@ Sir-myke现在应该修好了。 – icecub

+0

@ Sir-myke现在应该修复了。我忘了解决这两个代码。我的错。 – icecub

1

测试上面的脚本没有工作,所以我这样做了研究,发现什么是错的。使用下面的代码我测试了它和它的工作

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* Set DSN and Options */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
PDO::ATTR_PERSISTENT => true, 
PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Instantiate the PDO object */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set ID */ 
$id = 25; 

/* Set query */ 

$query = "update location j1 inner join location j2 on j1.id <= j2.id 
left outer join location j3 on j2.id < j3.id 
set j1.id = j2.id + 1 
where j1.id = $id and j3.id is null"; 

/* Prepare query */ 
$stmt = $pdo->prepare($query); 

/* Bind values */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Execute query */ 
try { 
    $stmt->execute(); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'ALTER TABLE location AUTO_INCREMENT = 1'; 

/* Query database */ 
try { 
    $pdo->query($query); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 

,或者你可以使用这个简单的代码

<?php 


include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php"); 

// Create connection 
$con = mysqli_connect($host, $user, $pass, $database); 


$id = 128; 

$Sql_Query = "update product set id=3 where id=1"; 

$Sql_Query = "update product j1 inner join product j2 on j1.id <= j2.id 
left outer join product j3 on j2.id < j3.id 
set j1.id = j2.id + 1 
where j1.id = $id and j3.id is null"; 

if(mysqli_query($con,$Sql_Query)) 
{ 
echo 'Record Updated Successfully'; 
} 
else 
{ 
echo 'Something went wrong'; 
} 

mysqli_close($con); 
?> 
+0

感谢它的工作 –

0

研究的代码,并使用一个最适合你的。修改表和它的所有作品

UPDATE jobs j1 
INNER JOIN jobs j2 ON j1.worker_id <= j2.worker_id 
LEFT OUTER JOIN jobs j3 ON j2.worker_id < j3.worker_id 
SET j1.worker_id = j2.worker_id + 1 
WHERE j1.worker_id = 3 AND j3.worker_id IS NULL; 
  • J1是要改变该行的基础上,WHERE子句中的条件。
  • j2是worker_id大于或等于j1的行集合。
  • j3是具有比j2更大的worker_id的行集合(当j2具有表中最大的worker_id时,j3没有行,所以j3。*将为NULL)。
  • j2因此具有最大的worker_id,所以使用它的值+ 1。

演示:

mysql> create table jobs (id serial primary key, worker_id int); 
mysql> insert into jobs (worker_id) values (1), (2), (3), (4), (5); 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   3 | 
| 4 |   4 | 
| 5 |   5 | 
+----+-----------+ 

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 3 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   5 | 
+----+-----------+ 

它的工作原理,无论价值,我们想改变:

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 5 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   7 | 
+----+-----------+ 

和它的作品,即使我们改变已经具有最高值的行在表中:

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 7 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   8 | 
+----+-----------+ 
相关问题