I have a booking system whereby a user adds 2 events to a cart. They then complete a single registration form where they enter name, email, num of children, and t-shirt sizes for each child. As there is only a single registration form for the 2 events T-shirt size is recorded twice per event.
I am trying to sum the number of t-shirts in the first event and exclude the t-shirt sizes in the second event.
The only field i can identify the duplicate row by is the email column in jos_eb_registrants.
There are 2 tables involved: jos_eb_field_values - records the t-shirt size jos_eb_registrants - records the id and event_id and maps them to the corresponding field values.
I have the following query The aim is to only count records where email only exists once therefore ignore rows if the email is the same) Im struggling to get the last part working:
AND `email` IN (SELECT `id`,COUNT(`email`)
FROM `jos_eb_registrants` GROUP BY `id` HAVING COUNT(`id`)= 1)
SELECT SUM(`field_value` = '5-6')
AS 'results1' ,SUM(`field_value` = '7-8')
AS 'results2' ,SUM(`field_value` = '9-11')
AS 'results3' ,SUM(`field_value` = '12-13')
AS 'results4' ,SUM(`field_value` = 'S (adult)')
AS 'results5' ,SUM(`field_value` = 'M (adult)')
AS 'results6' ,SUM(`field_value` = 'L (adult)')
AS 'results7'
FROM `jos_eb_field_values`
WHERE `registrant_id`
IN (SELECT `id` FROM `jos_eb_registrants`
WHERE `published` = 1
AND `event_id`
IN(4784,4785,4786,4787,4788,4789,4790,4791,4784,5412,5413,5414,5415,5420) )
AND `email`
IN (SELECT `id`,COUNT(`email`)
FROM `jos_eb_registrants`
GROUP BY `id`
HAVING COUNT(`id`)= 1)
Here are the 2 tables I only want to count field_value for registrant id 22434 and ignore 22435 in jos_eb_field_values table because they have the same email address in the jos_eb_registrants table.
Thanks for any pointers... For context here is the code with @UndercoverDog corrections:
$eventid = array(4784,4785,4786,4787,4788,4789,4790,4791,4784,5412,5413,5414,5415,5420);
$i = 0;
while($i < count($eventid))
{
$eventid[$i]."\n";
$sql = "SELECT SUM(field_value = '5-6')
AS 'results1' ,SUM(field_value = '7-8')
AS 'results2' ,SUM(field_value = '9-11')
AS 'results3' ,SUM(field_value = '12-13')
AS 'results4' ,SUM(field_value = 'S (adult)')
AS 'results5' ,SUM(field_value = 'M (adult)')
AS 'results6' ,SUM(field_value = 'L (adult)')
AS 'results7'
FROM jos_eb_field_values
WHERE `registrant_id`
IN (SELECT `id`
FROM `jos_eb_registrants`
WHERE `published` = 1
AND `event_id`
IN ($eventid) )
AND email
IN (SELECT `id`,COUNT(SELECT DISTINCT `email`
FROM `jos_eb_registrants`))
FROM `jos_eb_registrants`
GROUP BY `id`
HAVING COUNT(`id`)= 1)";