如何获取sql表中具有特定id与php的行之后的行数?我想获得ID为6的行之后的行数。
答案 0 :(得分:2)
Select count(*) from TableName where ID > 6
答案 1 :(得分:1)
用于计算ID大于6的行的查询的SQL,假设您的表名为table
且ID列名为id
,将为:
SELECT count(*) FROM table WHERE id > 6;
要从PHP执行此操作,您可以修改example in the docs以添加自己的查询。输出也可以调整为返回标量值。
<?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);
?>