user | SubmissionId | FormId | FieldName | FieldValue |
---|---|---|---|---|
bob | 2 | 16 | state | mystate1 |
bob | 2 | 16 | qda | myqad1 |
bob | 2 | 16 | city | mycity1 |
rob | 3 | 16 | state | mystate2 |
rob | 3 | 16 | qda | myqad1 |
rob | 3 | 16 | city | mycity1 |
jac | 4 | 16 | state | mystate1 |
jac | 4 | 16 | qda | myqad3 |
jac | 4 | 16 | city | mycity1 |
sara | 5 | 20 | state | mystate1 |
sara | 5 | 20 | qda | myqad1 |
sara | 5 | 20 | city | mycity2 |
I want to count the number of submissions where formid
is 16
, state
is mystate1
, qda
is myqad1
, and city
is mycity1
.
In my example, the count should be 1 because only SubmissionId
number 2
satisfies all requirements.
$query = $db->getQuery(true);
//$query->select('COUNT(*)');
$query->select($db->quoteName('FieldValue') . ' IN ("mycity1")');
$query->from($db->quoteName('#__rsform_submission_values'));
$query->where($db->quoteName('formid') .' IN ("16")');
$query->where($db->quoteName('FieldName') .' IN ("state","qda","city")');
$query->where($db->quoteName('FieldValue') . ' IN ("mystate1","myqad1","mycity1")');
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $value) {
foreach($value as $key => $data) {
echo $data."<br />";
}
}
Here's my attempted query, but it doesn't produce the count that I want:
SELECT COUNT(SubmissionId)
FROM my_submission_values
WHERE FormId = '16'
AND FieldName ='state'
AND FieldValue = 'mystate1'
AND FieldName = 'qda'
AND FieldValue = 'myqad1'
AND FieldName = 'city'
AND FieldValue ='mycity1'