mysqli根据列数量值重复行

woobm2wo  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(295)

我创建了一个简单的sql语句来显示客户从woo商业商店购买的产品。
这是伟大的工作,但我正在努力寻找一个sql函数,重复行的基础上的一个值,也许一个不存在的php是必需的,任何帮助都将是伟大的。
谢谢

CURRENT VIEW    

ORDER ID.  |   PRODUCT NAME.   | QTY
-------------------------------------
   123     |       APPLE       |  3
   124     |       ORANGE      |  2
   125     |       PEAR        |  1

DESIRED VIEW

ORDER ID.  |   PRODUCT NAME.   | QTY
-------------------------------------
   123     |       APPLE       |  3
   123     |       APPLE       |  3
   123     |       APPLE       |  3
   124     |       ORANGE      |  2
   124     |       ORANGE      |  2
   125     |       PEAR        |  1

CODE

<?php

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT wppc_wc_customer_lookup.customer_id, wppc_wc_customer_lookup.first_name, 
wppc_wc_customer_lookup.last_name, wppc_wc_customer_lookup.email, 
wppc_wc_order_stats.customer_id, wppc_wc_order_stats.total_sales, 
wppc_wc_order_stats.order_id, wppc_wc_order_stats.date_created, wppc_wc_order_stats.status, 
wppc_woocommerce_order_items.order_id, wppc_woocommerce_order_items.order_item_name, 
wppc_woocommerce_order_items.order_item_id, wppc_woocommerce_order_itemmeta.order_item_id, 
wppc_woocommerce_order_itemmeta.meta_id, wppc_woocommerce_order_itemmeta.meta_key, 
wppc_woocommerce_order_itemmeta.meta_value FROM wppc_wc_customer_lookup

LEFT JOIN wppc_wc_order_stats ON wppc_wc_customer_lookup.customer_id = 
wppc_wc_order_stats.customer_id

LEFT JOIN wppc_woocommerce_order_items ON wppc_wc_order_stats.order_id = 
wppc_woocommerce_order_items.order_id

LEFT JOIN wppc_woocommerce_order_itemmeta ON wppc_woocommerce_order_items.order_item_id = 
wppc_woocommerce_order_itemmeta.order_item_id

WHERE wppc_woocommerce_order_itemmeta.meta_key = '_qty' AND 
wppc_woocommerce_order_items.order_item_name = 'Product Name' AND 
wppc_wc_order_stats.status = 'wc-completed'

ORDER BY wppc_wc_order_stats.date_created DESC";

$result = $conn->query($sql);

echo "<table border=\"1\"><tr>";
echo "<tr><th>Order ID</hr><th>First Name</hr><th>Last Name</hr><th>Email</hr><th>Total</hr> 
<th>Order Date</hr><th>Product Name</hr><th>Quantity</hr>";

if ($result->num_rows > 0) {

// output data of each row
while($row = $result->fetch_assoc()) {

echo "<tr><td>".$row["order_id"]. " </td><td> " .$row["first_name"] . " </td><td> " . 
$row["last_name"] . " </td> <td> " . $row["email"] . " </td><td> &pound".$row["total_sales"] 
. " </td> <td>".$row["date_created"] . " </td> <td> ".$row["order_item_name"] . " </td> <td> 
".$row["meta_value"] . " </td></tr> ";

}

} else {
echo "0 results";
}

echo "</table>";

$conn->close();
?>

这是伟大的工作,但我正在努力寻找一个sql函数,重复行的基础上的一个值,也许一个不存在的php是必需的,任何帮助都将是伟大的。

e5njpo68

e5njpo681#

其思想是使用不同的where子句进行多个select查询;
例如,在这种情况下,我们需要这样的查询:

SELECT * FROM table WHERE QTY > 0
UNION ALL
(SELECT * FROM table WHERE QTY > 1)
UNION ALL
(SELECT * FROM table WHERE QTY > 2)
ORDER BY `ORDER ID`

所以我们可以创建mysql过程来根据最大数量生成这样的查询

CREATE PROCEDURE GetAllProducts()
BEGIN
    select max(QTY) - 1 into @maxQty from table;
    set @resQry = 'SELECT * FROM table WHERE QTY > 0';
    set @i = 0;

    label1:
    LOOP
        IF @i = @maxQty THEN
            leave label1;
        END IF;
        SET @i = @i + 1;
        SET @resQry = CONCAT(@resQry, ' UNION ALL (SELECT * FROM table WHERE QTY > ', @i, ')');
    END LOOP label1;
    SET @resQry = CONCAT(@resQry, ' ORDER BY `ORDER ID`');
    PREPARE stmt FROM @resQry;
    EXECUTE stmt;
END;

然后你可以调用这个过程

CALL GetAllProducts();
ldioqlga

ldioqlga2#

如果您运行的是mysql 8.0,那么可以通过递归查询来实现这一点。从现有查询开始,将是:

with recursive 
    data  as ( <... your query goes here ...> ),
    rcte as (
        select order_id, product_name, qty, 1 rn from data
        union all 
        select order_id, product_name, qty, rn + 1 from rcte where rn < qty
)
select order_id, product_name, qty from rcte

在早期版本中,可以使用数字表。下面是一个数量最多为3的示例:

select d.*
from ( <... your query goes here ...> ) d
inner join (select 1 qty union all select 2 union all select 3) n 
    on n.qty <= d.qty

您可以使用更多 union all 以处理更多的数量。

相关问题