如何在每个类别的加载页面中仅获取2个产品

qjp7pelc  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(256)

我想在加载此页面时为每个类别显示两个产品,但如果单击某个类别类型,则它将显示所有产品。
这是我的密码:

<?php
   $resultspi= $con->query ("SELECT * FROM products WHERE ORDER BY id ASC"); 
   if ($resultspi->num_rows > 0) {
   while($rowpi = mysqli_fetch_array ($resultspi, MYSQL_ASSOC)) { 
         $id = $rowpi['id'];
         $types = $rowpi['type'];
         $image = $rowpi['images'];
         $names = $rowpi['name'];
 ?>
      <div class="item <?php echo $types;?>">
           <a href="uploads/products/<?php echo $image;?>" data-rel="gallery[prodcuts]" title="<?php echo $names;?>" class="">
              <img src="uploads/products/<?php echo $image;?>" alt="<?php echo $names;?>" class="thumb mCS_img_loaded" style="visibility: visible; opacity: 1;">
              <p class="info"> </p>
           <div class="overlay">
              <span></span>
           </div>
            </a>
       </div>
<?php 
      }
    }
 ?>
wnrlj8wa

wnrlj8wa1#

我使用这个查询。这个代码对我有用。

SELECT p1.id, p1.type,p1.name,p1.images FROM products p1 JOIN products p2 ON p1.type = p2.product_type AND p2.id >= p1.id GROUP BY p1.id, p1.type HAVING COUNT(*) <= 2 ORDER BY type, id
uxh89sit

uxh89sit2#

更改您的查询改为尝试以下操作:选择*from products where order by id asc limit 2

o8x7eapl

o8x7eapl3#

SELECT P.Name, rs.*
FROM (
    SELECT [CustomerNo], PlanId, Rank()
        OVER (Partition BY PlanId
            ORDER BY [CustomerNo] DESC) AS Rank
    FROM CustomerOfPlan
    ) rs
    INNER JOIN [Plan] P ON P.Id = rs.PlanId
WHERE Rank <= 2

在这个示例中,我有11个计划和16000个客户计划,但结果有22条记录。。为什么?每个计划我需要两个客户计划。

相关问题