如何显示MySQL表中的内容,因为行数等于3的多个[关闭]

I have a MySQL table having many rows and my question is,

How to

i) Display rows from MySQL table as the row length is equal to multiples of 3

ii) In other case if row count not equal to multiples of 3 then display the row equal to nearest multiples of 3 for example, if the row length is 5 then display only first 3 rows.

for more clarification,

  1. If mysql_num_rows = 3, display all three rows.
  2. If mysql_num_rows = 5, display any three rows.
  3. If mysql_num_rows = 11, display any nine rows.

Use

   $res=mysqli_query(
$con,"SELECT * FROM tbl_name");
 $row=mysqli_num_rows($res);//calculating total row count

$lim=$row-$row%3;//deciding limit
$res1=mysqli_query(
$con,"SELECT * FROM tbl_name LIMIT $lim");
//echo '<br>'.$row1=mysqli_num_rows($res1);
while($show=mysqli_fetch_array($res1))
{
    echo '<br>'.$show['id'];
}

Execute the query & show your data

MySQL Only

SELECT (COUNT(*)-(COUNT(*)%3)) FROM tbl_name INTO @lim;

SELECT @rownum := 0;

SELECT t.*, @rownum := @rownum + 1 AS rowno FROM tbl_name t WHERE rowno <= @lim;

PHP & MySQL

$res=mysqli_query( $conn, "SELECT COUNT(*) AS cnt FROM tbl_name");

$rec=mysqli_fetch_array($res);

$lim=$rec["cnt"]-($rec["cnt"]%3);

$res=mysqli_query( $conn, "SELECT * FROM tbl_name LIMIT $lim");

You'll need to validate cnt and lim