MYSQL 限制结果

发布于 2024-12-01 16:50:11 字数 1610 浏览 0 评论 0原文

我的查询几乎已经完成,但还有最后一个问题需要解决。我的查询如下所示:

//execute the SQL query and return records
$result = mysql_query("SELECT SUM(ps_order_detail.product_weight) * IF( COUNT(ps_order_detail.id_order_detail) > 50, 1.2, 1 )
 as total_provision, COUNT(ps_order_detail.id_order_detail) as antal_ordrar, ps_customer.firstname
                   FROM ps_order_detail 
                  JOIN ps_orders ON ps_order_detail.id_order = ps_orders.id_order
                  JOIN ps_order_history ON ps_orders.id_order = ps_order_history.id_order
                  JOIN ps_customer ON ps_orders.id_customer = ps_customer.id_customer
                  WHERE MONTH(ps_order_history.date_add) = MONTH(CURDATE()) AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13'
                  GROUP BY ps_orders.id_customer
                  ORDER BY SUM(ps_order_detail.product_weight) DESC

");


echo "<br>";
echo ("<img src=chart.php>");
echo "<br>";
echo ("<img src=chart_prov.php>");

//fetch tha data from the database
while ($row = mysql_fetch_array($result))
{
   echo '<font size="3">';
   echo " Namn: ".$row{'firstname'}. "";
   echo " Provision: ".$row{'total_provision'}. "";
   echo " Abonnemang: ".$row{'antal_ordrar'}. "";
   echo '<br>';
   echo '</font size>';
}

这部分:

AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13'

如果订单状态为 4 或 13,则导致显示结果。问题是在后端我可以多次将其置于这些状态,所以如果我不小心的话更新订单时,会多次添加结果。

这是一种限制该功能仅处理最新更新(在同一个表中具有 date_add )或限制为 1 的方法吗?

I've almost done with my query, but there is still one last issue to resolve. My query looks like this:

//execute the SQL query and return records
$result = mysql_query("SELECT SUM(ps_order_detail.product_weight) * IF( COUNT(ps_order_detail.id_order_detail) > 50, 1.2, 1 )
 as total_provision, COUNT(ps_order_detail.id_order_detail) as antal_ordrar, ps_customer.firstname
                   FROM ps_order_detail 
                  JOIN ps_orders ON ps_order_detail.id_order = ps_orders.id_order
                  JOIN ps_order_history ON ps_orders.id_order = ps_order_history.id_order
                  JOIN ps_customer ON ps_orders.id_customer = ps_customer.id_customer
                  WHERE MONTH(ps_order_history.date_add) = MONTH(CURDATE()) AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13'
                  GROUP BY ps_orders.id_customer
                  ORDER BY SUM(ps_order_detail.product_weight) DESC

");


echo "<br>";
echo ("<img src=chart.php>");
echo "<br>";
echo ("<img src=chart_prov.php>");

//fetch tha data from the database
while ($row = mysql_fetch_array($result))
{
   echo '<font size="3">';
   echo " Namn: ".$row{'firstname'}. "";
   echo " Provision: ".$row{'total_provision'}. "";
   echo " Abonnemang: ".$row{'antal_ordrar'}. "";
   echo '<br>';
   echo '</font size>';
}

This part:

AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13'

causes the result to be displayed if the state of orders is either 4 or 13. The problem is that in the backend I can put it in those states many times, so if I'm not careful when updating orders, it will add the results more than one time.

Is it a way to limit that funktion to just handle latest update (have date_add in same table) or limit by 1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-12-08 16:50:11

尝试使用

 SELECT SUM(ps_order_detail.product_weight) * IF( COUNT(ps_order_detail.id_order_detail) > 50, 1.2, 1 )
 as total_provision, COUNT(ps_order_detail.id_order_detail) as antal_ordrar,    ps_customer.firstname
               FROM ps_order_detail 
              JOIN ps_orders ON ps_order_detail.id_order = ps_orders.id_order
              JOIN ps_order_history ON ps_orders.id_order = ps_order_history.id_order                  
              JOIN ps_customer ON ps_orders.id_customer = ps_customer.id_customer
              WHERE ps_order_history.id = (select ps_order_history.id from ps_order_history where ps_orders.id_order = ps_order_history.id_order and MONTH(ps_order_history.date_add) = MONTH(CURDATE()) AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13' order by ps_order_history.date_add desc limit 1)
              GROUP BY ps_orders.id_customer
              ORDER BY SUM(ps_order_detail.product_weight) DESC

也许会有帮助。

Try to use

 SELECT SUM(ps_order_detail.product_weight) * IF( COUNT(ps_order_detail.id_order_detail) > 50, 1.2, 1 )
 as total_provision, COUNT(ps_order_detail.id_order_detail) as antal_ordrar,    ps_customer.firstname
               FROM ps_order_detail 
              JOIN ps_orders ON ps_order_detail.id_order = ps_orders.id_order
              JOIN ps_order_history ON ps_orders.id_order = ps_order_history.id_order                  
              JOIN ps_customer ON ps_orders.id_customer = ps_customer.id_customer
              WHERE ps_order_history.id = (select ps_order_history.id from ps_order_history where ps_orders.id_order = ps_order_history.id_order and MONTH(ps_order_history.date_add) = MONTH(CURDATE()) AND (ps_order_history.id_order_state) = '4' OR (ps_order_history.id_order_state) = '13' order by ps_order_history.date_add desc limit 1)
              GROUP BY ps_orders.id_customer
              ORDER BY SUM(ps_order_detail.product_weight) DESC

Maybe it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文