如何从sql中跳过一行?

时间:2016-10-01 21:42:51

标签: php html mysql database web

嗨新手到php和mysql。试图找到如何在我的sql表的循环中按降序跳过第一个或最后一个值?我想先显示最近添加的行,但不显示最后添加的行。

<tr>
<?php
    $page=$_GET['pages'];    /* created in index.php(pages.php?pages=m_$title) */
    $nage="z_".$page;
    $query="SELECT * FROM `$nage` ORDER BY a_id DESC LIMIT 0, 1" ;
    $run=mysqli_query($conn,$query);
    while($row=mysqli_fetch_array($run)) {
        $page_title=$row['title'];
        $img_src=$row['img_src'];

        // $page_desc=$row['story'];

        echo  "<tr bgcolor='pink'>
                   <td style='width:40%'>
                       <div class='titleDivX'>
                           <img class='imgFrame'src='$img_src' alt='tasvir'/>
                           <h5> $page_title</h5>
                       </div>
                   </td>
                   <td bgcolor='yellow'>
                       <div class='titleDivX'> </div>
                   </td>
               </tr>";
    }
?>

1 个答案:

答案 0 :(得分:1)

您可以使用mysqli_num_rows描述的$run=mysqli_query($conn,$query); while($row=mysqli_fetch_array($run)) { 函数。如果要查找第一行或最后一行,则可以替换代码的这一部分

$run=mysqli_query($conn,$query);
$rowsCount=mysqli_num_rows($run);
$count = 0;
while($row=mysqli_fetch_array($run)) {
    $count++;       
    $isFirst = ($count == 1) ? true : false;
    $isLast = ($count == $rowsCount) ? true : false;

用这个

$isFirst

然后,您可以将$isLast<?php $page=$_GET['pages']; /* created in index.php(pages.php?pages=m_$title) */ $nage="z_".$page; $query="SELECT * FROM `$nage` ORDER BY a_id DESC LIMIT 0, 1" ; $run=mysqli_query($conn,$query); $rowsCount=mysqli_num_rows($run); $count = 0; while($row=mysqli_fetch_array($run)) { $count++; $isFirst = ($count == 1) ? true : false; $isLast = ($count == $rowsCount) ? true : false; if (!$isLast) { $page_title=$row['title']; $img_src=$row['img_src']; // $page_desc=$row['story']; echo "<tr bgcolor='pink'> <td style='width:40%'> <div class='titleDivX'> <img class='imgFrame'src='$img_src' alt='tasvir'/> <h5> $page_title</h5> </div> </td> <td bgcolor='yellow'> <div class='titleDivX'> </div> </td> </tr>"; } } ?> 变量用于IF语句。例如,如果您想跳过最后一行,则可以使用如下代码:

SELECT email_address, COUNT(o.order_id) as number_of_orders, sum((item_price-discount_amount)*quantity) As Total
FROM Customers c JOIN Orders o
ON c.customer_id = o.customer_id
JOIN (
    SELECT order_id, 
        CASE WHEN item_price < 400 THEN 0 ELSE item_price END,
        --other columns you need
    FROM order_items ) oi on oi.order_id =o.order_id
GROUP BY email_address
HAVING COUNT (o.order_id) > 1
ORDER BY number_of_orders DESC;