gpt4 book ai didi

mysql - 在 Sql 中执行 a/(a+b)

转载 作者:行者123 更新时间:2023-11-29 06:42:41 25 4
gpt4 key购买 nike

我正在执行以下代码,但我无法获得正确的百分比作为输出

Select
name,
fans,
useful,
funny,
(useful/(Select (useful+funny) from user)) As useful_percent
From user
Order by fans desc
limit 10

输出是这样的,

+-----------+------+--------+--------+----------------+
| name | fans | useful | funny | useful_percent |
+-----------+------+--------+--------+----------------+
| Amy | 503 | 3226 | 2554 | 36 |
| Mimi | 497 | 257 | 138 | 2 |
| Harald | 311 | 122921 | 122419 | 1381 |
| Gerald | 253 | 17524 | 2324 | 196 |
| Christine | 173 | 4834 | 6646 | 54 |
| Lisa | 159 | 48 | 13 | 0 |
| Cat | 133 | 1062 | 672 | 11 |
| William | 126 | 9363 | 9361 | 105 |
| Fran | 124 | 9851 | 7606 | 110 |
| Lissa | 120 | 455 | 150 | 5 |
+-----------+------+--------+--------+----------------+

有人请解释一下代码出了什么问题吗?

最佳答案

您的选择应该会导致错误,因为(Select (useful+funny) from user) 返回多行。

你可能想要

Select
name,
fans,
useful,
funny,
100.0 * useful/(useful+funny) As useful_percent
From user
Order by fans desc
limit 10

关于mysql - 在 Sql 中执行 a/(a+b),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50903146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com