2017-07-26 70 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 | 
+----+-----------+ 
相關問題