2014-03-07 53 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