如何在这种条件下编写SQL查询

时间:2017-06-21 12:13:20

标签: mysql html5

我正在建立一个电子商务网站,并坚持在一个地方编写SQL查询,因为我想实现一个函数,它将循环到两个地方,然后再次切换它!有点难以解释请检查我的代码



  <?php
      $ret=mysql_query("select * from products where seller_type='best' and status = 1");
      while ($row=mysql_fetch_array($ret))
      {?>
      <div class="item">
        <div class="products best-product">
          <div class="product">
            <div class="product-micro">
              <div class="row product-micro-row">
                <div class="col col-xs-5">
                  <div class="product-image">
                    <div class="image"> <a href="product-details.php?pid=<?php echo htmlentities($row['id']);?>"> <img src="assets/images/products/p26.jpg" alt=""> </a> </div>
                    <!-- /.image -->

                  </div>
                  <!-- /.product-image -->
                </div>
                <!-- /.col -->
                <div class="col2 col-xs-7">
                  <div class="product-info">
                    <h3 class="name"><a href="product-details.php?pid=<?php echo htmlentities($row['id']);?>"><?php echo htmlentities($row['productName']);?></a></h3>
                    <div class="rating rateit-small"></div>
                    <div class="product-price"> <span class="price"> $<?php echo htmlentities($row['productPrice']);?> </span> </div>
                    <!-- /.product-price -->

                  </div>
                </div>
                <!-- /.col -->
              </div>
              <!-- /.product-micro-row -->
            </div>
            <!-- /.product-micro -->

          </div>
          <div class="product">
            <div class="product-micro">
              <div class="row product-micro-row">
                <div class="col col-xs-5">
                  <div class="product-image">
                    <div class="image"> <a href="product-details.php?pid=<?php echo htmlentities($row['id']);?>"> <img src="assets/images/products/p27.jpg" alt=""> </a> </div>
                    <!-- /.image -->

                  </div>
                  <!-- /.product-image -->
                </div>
                <!-- /.col -->
                <div class="col2 col-xs-7">
                  <div class="product-info">
                    <h3 class="name"><a href="product-details.php?pid=<?php echo htmlentities($row['id']);?>"> <?php echo htmlentities($row['productName']);?></a></h3>
                    <div class="rating rateit-small"></div>
                    <div class="product-price"> <span class="price"> $<?php echo htmlentities($row['productPrice']);?>	 </span> </div>
                    <!-- /.product-price -->

                  </div>
                </div>
                <!-- /.col -->
              </div>
              <!-- /.product-micro-row -->
            </div>
            <!-- /.product-micro -->

          </div>
        </div>
      </div>
      <?php } ?>
&#13;
&#13;
&#13;

现在如果我使用LIMIT 2然后在下一个查询中我将如何跳过我已经使用的前2个!!提前致谢

2 个答案:

答案 0 :(得分:0)

您可以使用LIMIT 2 OFFSET 2

select * from products where seller_type='best' and status = 1 LIMIT 2 OFFSET 2


在下一个迭代中增加2到4的值,然后在下一个4到6中,依此类推。
所以最终你需要使Offset动态的值在每次迭代时增加2你的循环。

<?php
    $recordQuery=mysql_query("select count(*) from products where seller_type='best' and status = 1");
    $totalRecords=mysql_fetch_assoc($recordQuery);
    for ($i=0; $i<totalRecords; $i+=2) {
       $ret=mysql_query("select * from products where seller_type='best' and status = 1 LIMIT 2 OFFSET " . $i);
       $row1=mysql_fetch_array($ret);
       $row2=mysql_fetch_array($ret);
?>

       <div class="item">
         <div class="products best-product">
           <div class="product">
            <!--/.use data from $row1 here -->
           </div>
          <div class="product">
            <!--/.use data from $row here -->
          </div>
        </div>
       </div>

<?php
    }
?>

答案 1 :(得分:0)

"select * from products where seller_type='best' and status = 1 LIMIT 2 OFFSET 2"

Offset就是您所需要的。它告诉mysql查询应该以什么偏移量开始。

这是动态找到正确偏移量的规则:

(offset * limit ) - limit

示例您想要从第一行开始的2行

(1 * 2 ) - 2 = 0

因此,您的偏移量为0,查询为

"SELECT * FROM products WHERE seller_type='best' and status = 1 LIMIT 2 OFFSET 0"

你想第一次从第三个开始获得2行:

(2 * 2 ) - 2 = 2

所以你的Offset这次是2,查询就像是

"SELECT * FROM products WHERE seller_type='best' and status = 1 LIMIT 2 OFFSET 2"

实现就像在SQL查询结束时添加OFFSETLIMIT一样简单