开发者

MYSQL multiply if?

开发者 https://www.devze.com 2023-03-30 03:46 出处:网络
I have a query that looks like this: //execute the SQL query and return records $result = mysql_query(\"SELECT SUM(ps_order_detail.product_weight) as total_provision, COUNT(ps_order_detail.id_order_d

I have a query that looks like this:

//execute the SQL query and return records
$result = mysql_query("SELECT SUM(ps_order_detail.product_weight) 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开发者_StackOverflow_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
                  ");


//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>';
}

It works fine but what i wana do is if the result "antal_ordrar" is larger than say 50 i wana multiply the result "total_provision" with 1,2 (add an extra 20% to the result)

Im new on mysql and are kinda stuck. Any ideas?


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, ...


First, wrap this in a subquery:

SELECT 
   -- use the if statement
   IF( antal_ordrar > 50, total_provision * 1.2, total_provision ),
   antal_ordrar,
   firstname
FROM
   -- create a subquery so you can reuse the data.
   ( SELECT SUM(ps_order_detail.product_weight) as total_provision, 
            COUNT(ps_order_detail.id_order_detail) as antal_ordrar, 
            ps_customer.firstname as firstname
      FROM ps_order_detail 
         -- are you sure you need this many joins?     
         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
         -- group by normally uses the 'having' keyword.
         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 ) 
    -- an alias for your subquery so you can treat it like a table.
    tbl;

Just noticed this. You're using braces when you mean brackets: $row{'total_provision'} should be $row['total_provision'].


Try to add to select

if (antal_ordrar > 50, total_provision * 1.2, total_provision) as total_provision
0

精彩评论

暂无评论...
验证码 换一张
取 消