我有下一个代码:
$rez1 = mysqli_query($kon, "SELECT producten.id as prodID, producten.*, product_shop_tt.*, product_shop_tt.id AS prshID FROM producten
INNER JOIN product_shop_tt ON producten.id = product_shop_tt.product_id
WHERE product_shop_tt.shop_id = ". $red["id"] ."");
$brRez = mysqli_num_rows($rez1);
$i = 1;
while($red1 = mysqli_fetch_assoc($rez1)){
$rezOrdDet = mysqli_query($kon, "SELECT COUNT(producten.id) as count FROM order_details
INNER JOIN product_shop_tt ON order_details.product_shop_tt_id = product_shop_tt.id
INNER JOIN producten ON producten.id = product_shop_tt.product_id
WHERE product_shop_tt_id = ". $red1["prshID"] ." ORDER BY count DESC");
$br = mysqli_num_rows($rezOrdDet);
while($rez = mysqli_fetch_assoc($rezOrdDet)){
echo "Result : " . $rez["count"] . "<br />";
}
}
我的问题是我得到的结果。我希望得到按计数排序的结果,但我无法做到正确。我希望order_details表中最常出现的结果显示在其余部分之上。 我得到下一个结果(数字是计数值):
Result : 3
Result : 4
Result : 2
Result : 3
这不是正确的顺序。我想要像
这样的东西Result : 4
Result : 3
Result : 3
Result : 2
问题是我现在得到随机结果,我想要的是结果按count的值排序。第一张照片是我得到的,第二张照片是我想要的。
答案 0 :(得分:1)
尝试将count
放入`(倾斜)符号
$newArr = array();
$rez1 = mysqli_query($kon, "SELECT producten.id as prodID, producten.*,
product_shop_tt.*, product_shop_tt.id AS prshID FROM producten
INNER JOIN product_shop_tt ON producten.id = product_shop_tt.product_id
WHERE product_shop_tt.shop_id = ". $red["id"] ."");
$brRez = mysqli_num_rows($rez1);
$i = 1;
while($red1 = mysqli_fetch_assoc($rez1))
{
$rezOrdDet = mysqli_query($kon, "SELECT COUNT(producten.id) as `count` FROM order_details
INNER JOIN product_shop_tt ON order_details.product_shop_tt_id = product_shop_tt.id
INNER JOIN producten ON producten.id = product_shop_tt.product_id
WHERE product_shop_tt_id = ". $red1["prshID"] ." ORDER BY `count` DESC");
$br = mysqli_num_rows($rezOrdDet);
while($rez = mysqli_fetch_assoc($rezOrdDet))
{
$newArr[] = $rez["count"];
}
}
rsort($newArr);
答案 1 :(得分:1)
您是否尝试按订单数量订购产品?
尝试单个查询。如果没有看到您的架构,那么按照当前的查询会有点困难,但您可以尝试以下方法:
"SELECT producten.*, product_shop_tt.*, COUNT(order_details.id) AS order_count from producten
INNER JOIN product_shop_tt ON producten.id = product_shop_tt.product_id
LEFT JOIN order_details ON order_details.product_shop_tt_id = product_shop_tt.id
WHERE product_shop_tt.shop_id = '" . $red['id'] . "'
GROUP BY producten.id
ORDER BY order_count DESC"
答案 2 :(得分:1)
您应该使用单个查询 -
"SELECT p.id as prodID, p.*, s.*,
s.id AS prshID, COUNT(p.id) as res_count
FROM producten p
INNER JOIN product_shop_tt s ON p.id = s.product_id
INNER JOIN order_details o on o.product_shop_tt_id = s.id
WHERE s.shop_id = ". $red["id"] ."
ORDER BY res_count DESC"
答案 3 :(得分:0)
您还可以按顺序使用列序列号
和你一样,你可以使用order by 1 desc
此处按1表示结果的第一列,依此类推。