如何计算任意数量的 COUNTS 的总和
我想知道是否可以在一个 SQL 查询中将每张照片的分数乘以其分数类型系数的所有乘积求和?
示例:
t = total : my aim, the total score for each photo
c_foo = count score type foo = the amount of scores for each photo with the score type name = foo
c_bar = count score type bar = the amount of scores for each photo with the score type name = bar
m_foo = foo factor
m_bar = bar factor
不同分数类型的数量是任意的。
t = c_foo * m_foo + c_bar * m_bar + … + c_last * m_last
我最初的想法是使用以下表结构:
所以到目前为止,我有以下查询:
SELECT p.id, st.name, st.factor, COUNT(*) AS count
FROM s2p_photo p
LEFT JOIN s2p_score s
LEFT JOIN s2p_score_type st
GROUP BY p.id, st.name
ORDER BY p.id ASC
我收到照片的名称、系数和总和,但我无法进行数学计算。
我不知道是否UNION
这里是可行的。
我的表结构没问题吗?你们有人知道吗?
PS:抱歉,我无法发布图片;请在浏览器中手动打开它们:(
I'm wondering if it is possible to totalize all products of the amount of scores for each photo multiplied by their score type factor in one SQL query?
Example:
t = total : my aim, the total score for each photo
c_foo = count score type foo = the amount of scores for each photo with the score type name = foo
c_bar = count score type bar = the amount of scores for each photo with the score type name = bar
m_foo = foo factor
m_bar = bar factor
The number of different score types is arbitrary.
t = c_foo * m_foo + c_bar * m_bar + … + c_last * m_last
My initial idea is to use the following table structure:
So far I have the following query:
SELECT p.id, st.name, st.factor, COUNT(*) AS count
FROM s2p_photo p
LEFT JOIN s2p_score s
LEFT JOIN s2p_score_type st
GROUP BY p.id, st.name
ORDER BY p.id ASC
I receive the name, factor and sum of photos but I'm not able to do the math.
I have no idea if UNION
is feasible here.
Is my table structure ok? Does anyone of you has a clue?
PS: Sorry I'm not able to post images; please open them manually in your browser :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否明白你的意思,但这就是你要找的吗?
Not sure whether I understand what you mean, but is this what you are looking for?