How does mysql query their respective sum ('number') based on the Id array at once?

now there is an array. You need to find out the sum of the fields number when the field card_id equals each value of the array
. Finally, you want the result to look something like this:
card_id sum
1 10
2 20

.

currently, you can only traverse queries one by one, and you need to query the database many times: $this- > model- > where ("card_id", $id)-> sum (" number");
I don"t know if there is any way to query all at once

.
Jun.11,2021

select card_id,sum (number) from tablename where card_id in array_cardid group by card_id;


where id in (1 Magazine 2) get to know

Menu