从查询中的每个给定条件中选择相等的金额

$tags = explode(',' , $tags );
$query = " select * from table where 1=2  ";

foreach($tags as $tag )
$query .= " || `tags` = '$tag' ";

$query .=" order by id desc limit 3 ";

so lets say

$tags = array( a , b ,c );

and we have many rows with a , b , c as tag in database

now if the last 3 rows in database have tag=a then this query will return these 3 rows .

so i'll get 3 a tags and here is the problem i want to select equal amount of each given tag from db .. in this example a , b ,c not 3 a .

and here is the tricky part .... database may only have a , b tags .... in this case i want 2 a and 1 b ... or 1 a and 2 b

is there anyway to do this ?