我有movies
表
SELECT * FROM movies
+-----------------------------------+---------------------------------------------------------+
| MovieTitle | Awards |
+-----------------------------------+---------------------------------------------------------+
| Rocky 20 | Nominated for 1 Oscar. Another 37 wins & 46 nominations |
| Die Hard 51 | Won 1 Oscar. Another 5 wins & 19 nominations. |
| Killer tomatoes | 9 nominations. |
+-----------------------------------+---------------------------------------------------------+
我希望能SUM
Awards
+-----------------------------------+---------------------------------------------------------+-------+
| MovieTitle | Awards | Total |
+-----------------------------------+---------------------------------------------------------+-------+
| Rocky 20 | Nominated for 1 Oscar. Another 37 wins & 46 nominations | 84 |
| Die Hard 51 | Won 1 Oscar. Another 5 wins & 19 nominations. | 25 |
| Killer tomatoes | 9 nominations. | 9 |
+-----------------------------------+---------------------------------------------------------+-------+
中所有奖项/提名的数量,以便有类似的内容:
{{1}}
关于如何只使用MySQL实现这一目标的任何建议?
答案 0 :(得分:2)
你的字符串都在寻找“提名”,“胜利”和“奥斯卡”之前的数字。这简化了问题。
您可以使用字符串操作获取所需的值。这是一个解决方案:
select ((case when awards like '% nominations%'
then substring_index(substring_index(awards, ' nominations', 1), ' ', -1) + 0
else 0
end) +
(case when awards like '% wins%'
then substring_index(substring_index(awards, ' wins', 1), ' ', -1) + 0
else 0
end) +
(case when awards like '% Oscar%'
then substring_index(substring_index(awards, ' Oscar', 1), ' ', -1) + 0
else 0
end)
) as TotalMentions
我可以理解必须处理关系数据库中格式不正确的数据。在这种情况下,字符串形成得很好,所以解决方案并非那么不合理。
答案 1 :(得分:-1)
SELECT SUM(column_name) FROM table_name;
或
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
我希望这有帮助!