I would strongly suggest to separate the products and not to put them all into one array. Or even to create a taxonomy companies, but this is more up to the while design and maybe a bad idea. Nevertheless this is a nice question to solve, so let’s play.
So, the 'prefix_products'
meta key is always an array of products. Now you need all companies, which sell a product with a certain ID.
This should do the trick:
function get_all_companies_selling( $product_id ){
global $wpdb;
$sql = "select post_id from " . $wpdb->prefix . "postmeta where
meta_key = 'prefix_products' &&
meta_value like '%%%s%%'";
$product_id = 's:' . strlen( $product_id ) . ':"' . (int) $product_id . '";';
$sql = $wpdb->prepare( $sql, $product_id );
$res = $wpdb->get_results( $sql );
return $res;
}
get_all_companies_selling( 331 );