如果仅当数组中存在当前字段时,如何计算或求和字段

时间:2018-02-08 21:24:12

标签: mysql sql database mongodb

我有以下查询:

SELECT 
count(form_rows.field_name), 
form_rows.field_name, 
sum(form_rows.field_value) as sales, 
form_Submissions.id as fsId 
FROM form_submissions
LEFT JOIN form_rows on form_Submissions.id = form_rows.form_submission_id
WHERE form_submissions.organization_id = 10
GROUP BY form_rows.field_name  

重现以下结果: enter image description here

问题:基于SUM

COUNTfield_name是否有办法

澄清:

说我有一个数组:["ssn", "price"]

我想仅将salesfield_names存在于给定数组中

1 个答案:

答案 0 :(得分:0)

这不是这样做的吗?

SELECT 
count(form_rows.field_name), 
form_rows.field_name, 
sum(form_rows.field_value) as sales, 
form_Submissions.id as fsId 
FROM form_submissions
LEFT JOIN form_rows on form_Submissions.id = form_rows.form_submission_id
WHERE form_submissions.organization_id = 10

AND form_rows.field_names in ('ssn','price')

GROUP BY form_rows.field_name