MYSQL中的乘法,除法和加法

时间:2013-12-18 17:19:27

标签: php mysql sql arithmetic-expressions

我正在进行A-B测试,看看哪种样式的网页可能会获得更多订单的产品,并试图将一个页面称为“更好”,因为它更频繁地显示。

我的MYSQL数据库中有8列。

  1. Orders_Page_One
  2. Orders_Page_Two
  3. Orders_Page_Three
  4. Visitors_Page_One
  5. Visitors_Page_Two
  6. Visitors_Page_Three
  7. Total_Orders
  8. Total_Visitors
  9. 目前我只运行查询:

    
    $result = mysql_query("SELECT *, (Total_Orders / Total_Visitors) AS order_percent FROM orders WHERE category = clothes ORDER BY order_percent DESC LIMIT 5") or die(mysql_error());
    

    这可行但

    这允许我显示哪些订单最受欢迎,但不考虑它们出现在哪个页面上。

    我试过了:

    
    $result = mysql_query("SELECT *, (Orders_Page_One / Visitors_Page_One) + (Orders_Page_Two / Visitors_Page_Two) + (Orders_Page_Three / Visitors_Page_Three) AS order_percent FROM orders WHERE category = clothes ORDER BY order_percent DESC LIMIT 5") or die(mysql_error());
    

    但这似乎没有给出准确的结果。

    我也试过

    
    $result = mysql_query("SELECT *, ((Orders_Page_One / Visitors_Page_One) + (Orders_Page_Two / Visitors_Page_Two) + (Orders_Page_Three / Visitors_Page_Three)) AS order_percent FROM orders WHERE category = clothes ORDER BY order_percent DESC LIMIT 5") or die(mysql_error());
    

    但这会产生语法错误。

    假设:

    Orders Page One = 10 and Visitors Page One = 20 
    Orders Page Two = 10 and Visitors Page Two = 40 
    Orders Page Three = 10 and Visitors Page Three = 50 
    

    我想要的数学结果是:

    (10 / 20) + (10 / 40) + (10 / 50)
    

    OR

    0.5 + 0.25 + 0.20 = 0.95
    

    有什么建议吗?我想我只是没有正确设置数学。

    经过多次阅读后,我想我需要使用多项选择:

    IE:

    
        Select (orders_page_1/visitors_page_1) as order_percent_1 from orders
        Select (orders_page_2/visitors_page_2) as order_percent_2 from orders
        Select (orders_page_3/visitors_page_3) as order_percent_3 from orders
        Select (order_percent_1 + order_percent_two + order_percent_3) as order_percent
        WHERE category = clothes
        ORDER BY order_percent DESC
    

    但格式不确定?或者,如果这可行?

    $result = mysql_query("
    
    SELECT * FROM orders, (
        SELECT (orders_page_1 / visitors_page_one) AS ord1 FROM orders WHERE category = 'clothing' ORDER BY ord1 DESC LIMIT 2) AS ord_1,
          (
        SELECT (orders_page_2 / visitors_page_two) AS ord2 FROM orders WHERE category = 'clothing' ORDER BY ord2 DESC LIMIT 2) AS ord_2,
          (
        SELECT (orders_page_3 / visitors_page_3) AS ord3 FROM orders WHERE category = 'clothing' ORDER BY ord3 DESC LIMIT 2) AS ord_3,
    
        ORDER BY (ord_1 +ord_2 + ord_3) DESC LIMIT 2") or die(mysql_error());
    

    然而,这是错误:

    您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第8行的“ORDER BY(ord_1 + ord_2 + ord_3)DESC LIMIT 2”附近使用正确的语法

1 个答案:

答案 0 :(得分:1)

一个可能的问题是

WHERE category = clothes

应该是

WHERE category = 'clothes'

立即尝试,看看你得到了什么。