【问题标题】:How to select first row then other rows from a single mysql query? [duplicate]如何从单个 mysql 查询中选择第一行然后选择其他行? [复制]
【发布时间】:2019-03-12 13:24:15
【问题描述】:

我可能遗漏了显而易见的信息,如何返回我需要的结果?看看这个简单的例子:


$fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
$stmt_img = $db->prepare($fetch_image);
$stmt_img->execute();
$stmt_img_result = $stmt_img->get_result();

while ($row_img = $stmt_img_result->fetch_assoc()) {
     $image = $row_image['image_url'];
     echo 'Image url: '.$image.'<br>'; //this will echo out 10 times.
}

以上代码遍历所有 10 行。如何从结果集中返回第一行,然后返回其他 9 行?

编辑:我正在尝试创建一个图像预览器,以便第一张图像显示为占位符/主图像,然后其他 9 行显示为缩略图,如下所示:

<div class="sp-wrap">
            <a href="images/1.jpg">
                <!-- This would be the placeholder/main/first image from the mysql result set -->
                <img src="images/1_tb.jpg" alt="">
            </a>
            <!-- In real life these would be dynamically generated -->
            <a href="images/3.jpg"><img src="images/3_tb.jpg" alt=""></a>
            <a href="images/4.jpg"><img src="images/4_tb.jpg" alt=""></a>
            <a href="images/5.jpg"><img src="images/5_tb.jpg" alt=""></a>
            <a href="images/6.jpg"><img src="images/6_tb.jpg" alt=""></a>
        </div>

【问题讨论】:

  • 在你的表中创建 imageType 列然后使用这个查询 SELECT image_url FROM table order by imageType
  • 除了 SQL 查询之外,我想知道如何在结果集中执行它,即 while 循环
  • 将 imageType 值保存在数据库 1 中,用于您想要显示的第一张图片
  • 一个简单的布尔值 $first_row,最初设置为 true,然后在循环中检查就可以完成这项工作。
  • 如果行与行的 DOM 相同,但只有“样式”(图像尺寸)发生变化,您就不能把所有工作都交给 css 吗?

标签: php mysql


【解决方案1】:

一个简单的布尔变量可以区分第一行:

$first_row = true;

while ($row_img = $stmt_img_result->fetch_assoc()) {
     if ($first_row) {
         echo 'the first row';
         $image = $row_image['image_url'];
         echo 'Image url: '.$image.'<br>';
         $first_row = false;
     } else {
         $image = $row_image['image_url'];
         echo 'Image url: '.$image.'<br>'; //this will echo out 9 times.
     }
}

【讨论】:

    【解决方案2】:

    您可以尝试获取第一行,然后是其余的。但是您需要在 SQL 语句中使用 ORDER BY 子句,该子句将在每次执行中以相同的方式对行进行排序。

    <?php
    $fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
    $stmt_img = $db->prepare($fetch_image);
    $stmt_img->execute();
    $stmt_img_result = $stmt_img->get_result();
    
    // Fetch first row
    if ($row_first = $stmt_img_result->fetch_assoc()) {
         $image = $row_first['image_url'];
         echo 'Image url: '.$image.'<br>'; 
    }
    
    // Fetch next rows
    while ($row_next = $stmt_img_result->fetch_assoc()) {
         $image = $row_next['image_url'];
         echo 'Image url: '.$image.'<br>'; 
    }
    ?>
    

    另一种可能的方法是对第一行实施自定义检查:

    <?php
    $fetch_image = "SELECT image_url FROM table"; //returns about 10 rows
    $stmt_img = $db->prepare($fetch_image);
    $stmt_img->execute();
    $stmt_img_result = $stmt_img->get_result();
    
    $first = true;
    while ($row_img = $stmt_img_result->fetch_assoc()) {
       if ($first) {
          // Specific output for the first row
          $first = false;
          $image = $row_image['image_url'];
          echo 'Image url: '.$image.'<br>'; // this will echo out 1 time.
       } else {
          // Specific output for the next rows
          $image = $row_image['image_url'];
          echo 'Image url: '.$image.'<br>'; // this will echo out count - 1 times.
       }
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      在你的表中创建imageType Column 然后使用这个查询

      SELECT image_url FROM table order by imageType`
      

      为要在循环中首先出现的图像赋予值 1。

      如果您的第一行包含第一张图片,那么您不需要 imageType 列。你可以简单地使用

      SELECT image_url FROM table order by id
      

      id 是你的主键

      但是,如果您的记录将来发生变化,上述方法仍然是一种更加动态的方法。最好给他们提供 imageType 列以供将来使用,这是更合适的方法,而不是仅仅从数据库中获取第一条记录。

          $stmt_img = $db->prepare($fetch_image);
          $stmt_img->execute();
          $stmt_img_result = $stmt_img->get_result();
      
        if ( $firstrow = mysql_fetch_assoc($stmt_img_result) ) {
              $image = $firstrow['image_url'];
               echo 'Image url: '.$image.'<br>'; //this will echo out 1 times.
      
          while($row = mysql_fetch_assoc($stmt_img_result)) {
      
      
            $image = $row['image_url'];
               echo 'Image url: '.$image.'<br>'; //this will echo out 9 times.
      
            }
      
          }
      

      【讨论】:

      • 这可能有效,但它仍然需要一个不必要的数据库列,而它也可以用 PHP 完成。 (但我没有投反对票)
      • @BillWhickomb 感谢您告诉我。到目前为止,我在 StackOverflow 上学到的是告诉 OP 如何正确地做事,而不是仅仅为他们的问题提供解决方案。即使它在数据库中增加了一个额外的列也是正确的,这是他将来需要的。我还给出了另一种解决方案,没有添加额外的列,即按主键排序
      • 我不认为这是完成这项任务的“正确”方式。如果我是为了工作而做这个任务,我可能会选择 Zhorov 的第一个代码块,因为它没有在循环内执行任何浪费的条件检查。
      • @mickmackusa 如果他获取管理员图像的记录在未来发生更改或被删除怎么办?
      • 我不明白这是怎么回事。 OP 想要对第一行做一些事情,然后对所有后续行做一些不同的事情。没有理由在这里发明边缘案例。
      猜你喜欢
      • 2016-06-15
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多