I want to show two products for each category in loading this page but If I click a category type then it will be showing all of the products.
Here is my code:
<?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
}
}
?>
I use this query. This code work for me.
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
Change your query try this instead: SELECT * FROM products WHERE ORDER BY id ASC LIMIT 2
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
In this sample I have 11 Plans and 16000 CustomerOfPlans but result has 22 records.. why ? I need 2 CustomerOfPlan per Plan.