我需要对除第一列以外的一行求和。
也有类似的东西:
选择总和(col2 + col3 + col4 +colN) 从数字 WHERE 用户名 = '人';
我的表将不断地添加列。所以我希望它也能自动获取新列的总和,而无需将其硬编码到查询中?
user_name | Col | col2 | Col3 | Col4 + other columns.
person1 | 2 | 3 | 76 | 56 etc. ---------> sum of row
person2 | 6 | 72 | 200 | 13 etc. ---------> sum of row
在此先感谢您的帮助!
最佳答案
不想“回避”这个问题,但看起来您可以使用不同的数据结构。
您应该考虑拥有一个包含 id
和 user_name
列的“用户”表,以及一个包含当前表中每个其他列的行(Col1
、Col2
... ColN
)。然后,新表将有一个用于 user_name 的列,以将其链接到用户表。
这样你就可以做类似的事情:
SELECT SUM(property_column) FROM properties WHERE user_name = <RequiredUserName>
我还建议按 ID 选择用户(即 properties
表包含 user_id 列,而不是 user_name 列),除非您确信 user_name 永远不会改变(甚至那时......)。
关于php - 如何对一行中除ID以外的所有列的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10280183/