PostgreSQL: Array of LIKEs
Now you may ask what is wrong with that? Well on its own yes, nothing…. but what about when we need to search for 5 product conditions at once? The query will get just kind of ugly and unwieldy!
1234567891011SELECTsum(product_cost)FROMt_testWHEREproduct_codeLIKE'%123%'ORproduct_codeLIKE'%234%'ORproduct_codeLIKE'%345%'ORproduct_codeLIKE'%456%'ORproduct_codeLIKE'%567%';Not something I normally enjoy writing…so can we do better? Yes, we can! Say hello to our “array of LIKEs”:
1234567SELECTsum(product_cost)FROMt_testWHEREproduct_codeLIKEANY( array['%123%','%234%','%345%','%456%','%567%']);-- or the same using shorter Postgres array notationSELECTsum(product_cost)FROMt_testWHEREproduct_codeLIKEANY(‘{%123%,%234%,%345%,%456%,%567%}’);
source:
$link[host]
Read Original Source