如何获得SQL表中具有php特定id的行之后的行数?我想要得到id为6的行后面的行数
Select count(*) from TableName where ID > 6
计算ID大于6的行数(假设表名为table
, ID列名为id
)的查询的SQL如下:
SELECT count(*) FROM table WHERE id > 6;
要在PHP中完成此操作,您可以修改文档中的示例以添加您自己的查询。输出也可以调整为返回一个标量值。
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Performing SQL query
$query = 'SELECT count(*) FROM table WHERE id > 6';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>'n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "'t<tr>'n";
foreach ($line as $col_value) {
echo "'t't<td>$col_value</td>'n";
}
echo "'t</tr>'n";
}
echo "</table>'n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>