是否可以在MySQL中使用SUBSTR
和GROUP BY
仅对前两个字符进行SUM?
如:
SELECT SUM(Substr(period, 1,2) FROM table GROUP BY period
我试过这些SQL。但由于使用GROUP BY
而没有效果。这些查询的结果是所有周期值的总和。
答案 0 :(得分:0)
试试这个(如果你想要将两个第一个字符相加):
SELECT
SUM(CAST(Substr(period, 1, 1)) AS int) + CAST(Substr(period, 2, 1)) AS int))
FROM
table
GROUP BY
period
或
SELECT
SUM(CAST(Substr(Period, 1, 2) AS int))
FROM
table
GROUP BY
period
答案 1 :(得分:0)
您可以改为使用ASCII
个字符值:
SELECT
ASCII(Substr(period, 1,1)) + ASCII(Substr(period, 2, 1))
FROM
test group by period;