似乎无法使用avg()来处理sql中的case

时间:2015-05-15 16:47:04

标签: sql sql-server sqlite

我想将平均值()与大小写一起使用,但是下面的命令似乎给了我一个错误,但我不知道为什么

SELECT 
avg(case when price between 0 AND 9 then price end),
avg(case when price between 10 AND 19 then price end),
avg(case when price between 20 AND 29 then price end),
avg(case when price between 30 AND 39 then price end),
avg(case when price between 40 AND 49 then price end)
FROM products;

数据库看起来像这样:

ProductID   ProductName                    Price
1           Chais                           18 
2           Chang                           19 
3           Aniseed Syrup                   33
8           Northwoods Cranberry Sauce      40 
9           Mishi Kobe Niku                 97 

1 个答案:

答案 0 :(得分:2)

也许你需要这样的东西:

    $conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

$rows = $result->fetch_all(MYSQLI_ASSOC);