2014-03-07 96 views
0

我試圖總結我上次開始如何自動生成序列號的步驟,如此處所示Auto generate serial numbers。就我自己而言,我已經能夠提出一個代碼給我我想要的東西。回覆:自動生成自動增量編號

使用這行代碼

SELECT COUNT(donor_id) + 1 AS Counter 
    FROM tbl_donors 

我想是6 + 1的值;即6是記錄的總數,1是新記錄的附加數。但現在如何將它添加到將顯示這裏看到的所有值的表集http://www.netdataflow.com/rbme/是我遇到問題的地方。我使用Dreamweaver來建立我的桌面,下面是桌面代碼

<div align="center"> 
    <table width="1100" border="0" cellpadding="0" cellspacing="0"> 
    <tr id="colhead"> 
     <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong> 
</div></td> 
     <td width="200" height="30" id="labels"><div align="center"><strong>Donor 
Name</strong></div></td> 
     <td width="100" height="30" id="labels"><div align="center"> 
<strong>Designation</strong></div></td> 
     <td width="250" height="30" id="labels"><div align="center"> 
<strong>Address</strong></div></td> 
     <td width="80" height="30" id="labels"><div align="center"><strong>City</strong> 
</div></td> 
     <td width="80" height="30" id="labels"><div align="center"><strong>State</strong> 
</div></td> 
     <td width="80" height="30" id="labels"><div align="center"> 
<strong>Country</strong></div></td> 
     <td width="100" height="30" id="labels"><div align="center"><strong>Phone</strong> 
</div></td> 
     <td width="150" height="30" id="labels"><div align="center"><strong>Email 
Address</strong></div></td> 
    </tr> 
    <?php do { ?> 
     <tr <?php 
// technocurve arc 3 php bv block2/3 start 
echo " style=\"background-color:$color\""; 
// technocurve arc 3 php bv block2/3 end 
?> id="rowlines"> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['address']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['city']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['state']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['country']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['phone']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['emailaddr']; ?> 
</div></td> 
     </tr> 
     <?php 
// technocurve arc 3 php bv block3/3 start 
if ($color == $color1) { 
$color = $color2; 
} else { 
$color = $color1; 
} 

任何人都可以幫忙嗎?如果可能的話,我會想添加到

SELECT COUNT(donor_id) + 1 AS Counter 
FROM tbl_donors 

到表格的第一行之前的ID。我願意接受任何其他的想法或方式,或者可以手動編碼表格,而不是使用Dreamweaver動態表格集。

我很感激你提前幫忙。

邁克

+0

我不明白你所說的「汽車」 – Strawberry

+3

的定義現場處理這個? –

+0

@Strawberry:對不起,重複。我只是想說自動生成或auto_increment號碼 –

回答

0

我已經能夠得到正確的代碼,我的問題上「自動生成自動遞增的數字」。我認爲我的問題是我的問題的標題。正確的標題應該是「如何生成錶行號」

無論如何找到下面我使用的代碼。下面是引用鏈接:With MySQL, how can I generate a column containing the record index in a table?

SELECT d.*, @curRow := @curRow + 1 AS row_number 
FROM tbl_donors d 
JOIN (SELECT @curRow := 0) r 
WHERE user_name = %s" **//*Please dont add this WHERE clause if you don't need it. In my case I filter records based on the person who entered it hence the WHERE clause** 

而是因爲我使用Dreamweaver,Dreamweaver將它改成如下格式:

mysql_select_db($database_yourdatabasename, $yourdatabasename); 
$query_rsdonors = sprintf("SELECT d.*, @curRow := @curRow + 1 AS row_number 
FROM tbl_donors d JOIN (SELECT @curRow := 0) r WHERE user_name = %s", 
GetSQLValueString($colname_rsdonors, "text")); 
$query_limit_rsdonors = sprintf("%s LIMIT %d, %d", $query_rsdonors, $startRow_rsdonors, 
1$maxRows_rsdonors); 
$rsdonors = mysql_query($query_limit_rsdonors, $ProjMonEva) or die(mysql_error()); 
$row_rsdonors = mysql_fetch_assoc($rsdonors); 

下面是表的代碼結構

<div align="center"> 
    <table width="1130" border="0" cellpadding="0" cellspacing="0"> 
    <tr id="colhead"> 
     <td width="30" height="30" id="labels"><strong>SN</strong></td> 
     <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong> 
</div></td> 
     <td width="200" height="30" id="labels"><div align="center"><strong>Donor 
Name</strong></div></td> 
     <td width="100" height="30" id="labels"><div align="center"> 
<strong>Designation</strong></div></td> 
     <td width="250" height="30" id="labels"><div align="center"> 
<strong>Address</strong></div></td> 
     <td width="80" height="30" id="labels"><div align="center"><strong>City</strong> 
</div></td> 
     <td width="80" height="30" id="labels"><div align="center"><strong>State</strong> 
</div></td> 
     <td width="80" height="30" id="labels"><div align="center"> 
<strong>Country</strong></div></td> 
     <td width="100" height="30" id="labels"><div align="center"><strong>Phone</strong> 
</div></td> 
     <td width="150" height="30" id="labels"><div align="center"><strong>Email 
Address</strong></div></td> 
    </tr> 
    <?php do { ?> 
     <tr <?php 
// technocurve arc 3 php bv block2/3 start 
echo " style=\"background-color:$color\""; 
// technocurve arc 3 php bv block2/3 end 
?>id="rowlines"> 
     <td id="labels"><?php echo $row_rsdonors['row_number']; ?></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['address']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['city']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['state']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['country']; ?> 
</div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['phone']; ?></div> 
</td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['emailaddr']; ?> 
</div></td> 
     </tr> 
     <?php 
// technocurve arc 3 php bv block3/3 start 
if ($color == $color1) { 
$color = $color2; 
} else { 
$color = $color1; 
} 

這裏我得到的簡要說明

SN  Donor Name    Designation 
1 Mr Michael Nwuzor  Chief Consultant 
2 Mr Michael Nwuzor  Chief Consultant 
3 South-Sea Datcomm Ltd 

這裏是我能夠得到正確的代碼,其中的鏈接:爲什麼你認爲你的鱈魚是不是讓數據庫自動增量更好With MySQL, how can I generate a column containing the record index in a table?

0

只取一個變量「$計數器」,並以此來顯示序列號

我加入了只有一個使用的代碼示例。請通過它。

<div align="center"> 
    <table width="1100" border="0" cellpadding="0" cellspacing="0"> 
    <tr id="colhead"> 
    <td width="30" height="30" id="labels"><div align="center"><strong>SI NO.</strong>  
     </div></td> 
     <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong> 
     </div></td> 
     <td width="200" height="30" id="labels"><div align="center"><strong>Donor Name</strong></div></td> 
     <td width="100" height="30" id="labels"><div align="center"><strong>Designation</strong></div></td> 
    </tr> 
    <?php 
    $counter = 1; 
      for($i=0;$i<$total_rec;$i++) //"$total_rec" is the total number of records found in your required table 
    { 
?> 
    <tr id="rowlines"> 
     <td id="labels"><div align="center"><?php echo $counter; ?></div></td> 
    <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?></div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?></div></td> 
     <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?></div></td> 
    </tr> 
<?php 
    $counter++; 
} 
?> 

+0

嗯,意大利麪條,愛他們。 –

+0

@Andolaso​​ft:謝謝你的貢獻。我遵循你的步驟,但重複了序列號。不過,我使用關鍵字「行號」搜索了「在mysql中生成行號」並獲得了此頁「http://stackoverflow.com/questions/3126972/with-mysql-how-can-i-generate- a-column-containing-the-record-index-in-a-table「從我能夠得到正確的代碼。我將編輯上面所做的更新以顯示正確的代碼。 –

+0

@Phince Michael:我已經更新了正確的代碼。請看看,現在你不會得到序列號的重複。 – Andolasoft