I'm fairly new to using cases in selection statements so I need a bit of help.
I have two tables. Lets call one res and one ser (for resource and serial).
res contains a field id & bar_code_id
ser contains a field id & container_id and they join on the id obv.
Anyway, what I need to do is figure out how to list all of the bar_code_id's of every item who's container_id is equal to a specific bar_code_id's res.id field.
For example:
the container I have bar_code_id is 123456. It is a container and its "container_id" is null, because it cant be placed in the container. the container has several items inside of it, however, the only way I can find the items is by finding every item whos container_id is the same as 123456's res.id
I'm not sure how to tackle this with case statements. I tried something along the lines of:
select case
when res.bar_code_id is '123456'
then (select res.bar_code_id from res
left outer join ser on ser.id = res.id
where ser.container_id = <somehow the variable of 123456's res.id?>) end
from res
But I know I'm way off. I'm not sure how I would store the res.id as a variable to use later in the comparison of ser.container_id = res.id part...
Anyway I hope thats all fairly clear. If anything is confusing let me know & I appreciate the help.
Edit: Here's what I ended up doing that worked, should anyone need this in the future
set @barcode := 123456;
set @id := (select res.id from res where res.bar_code_id = @barcode);
select res.id, resource_name, res.bar_code_id, container_id from res
left outer join ser
on ser.item_id = res.id
where container_id = @id