php - MYSQL 中的乘法、除法和加法

标签 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

目前我只运行一个查询:


$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)

0.5 + 0.25 + 0.20 = 0.95

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

经过更多阅读,我认为我需要使用多重选择:

即:


    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”附近使用的正确语法

最佳答案

一个可能的问题是

WHERE category = clothes

应该是

WHERE category = 'clothes'

现在试试吧,看看你会得到什么。

关于php - MYSQL 中的乘法、除法和加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664424/

相关文章:

mysql - mysql中从一张表导入到另一张表

mysql - Zend db 更新值未设置

sql - MySQL 选择链接行

java - 无法创建池 Async Jersey+Spring+Tomcat 的初始连接

mysql - 对多个 JOIN 表使用 COUNT 和 GROUP BY

javascript - 通过 db.transaction 函数传递参数

php - foreach 不填充数组

PHP 代码 (mysqli) 查询在特定代码后不起作用

PHP - 调试 curl

php - mysql不是用PHP编译的吗?