I get the error "Subquery returns more than 1 row" when I run the following statement.
SELECT `labels`.*,
IF((SELECT `label_id`
FROM `customer_print_sessions`
WHERE `customer_id` = (SELECT `customer_id`
FROM `customer_accounts_logins`
WHERE `id` = '1069')
AND `customer_print_sessions`.`label_id` = `labels`.`id`) IS
NOT NULL
, 'Y', 'N') `in_queue`
FROM `labels`
WHERE `id` IN (SELECT `label_id`
FROM `customer_groups_label_relation`
WHERE `group_id` IN (SELECT `group_id`
FROM `customer_accounts_group_relation`
WHERE `customer_id` =
(SELECT
`customer_id`
FROM
`customer_accounts_logins`
WHERE `id` =
'1069'
)))
ORDER BY `labels`.`plu` ASC;
I ran each SELECT statement separately to determine which one returns multiple results that this statement as a whole was not expecting. I've determined that the following part of the above statement is the part that is returning multiple results which aren't expected.
SELECT `label_id`
FROM `customer_print_sessions`
WHERE `customer_id` = (SELECT `customer_id`
FROM `customer_accounts_logins`
WHERE `id` = '1069')
AND `customer_print_sessions`.`label_id` = `labels`.`id`
How can I write this so that the IF function and the whole statement will work with the multiple results that are being returned?
UPDATE: Since the above statement returns multiple results of the same value, is there a way I can write it to just return one?
Best Answer
I added
GROUP BY label_id
to make the original statement work.