There are times we need to query from database where the record is from ‘A’ to ‘M’, or from ‘G’ to ‘L’ or from ‘T’ to ‘Z’ or from.. well, you understand what I mean. To select a record range between 2 alphabets ,we can have a query like this :
$tenant_query=mysql_query(”SELECT t.*, c.* FROM tenant t, category c WHERE t.category__id=c.category_id AND LEFT(t.tenant_name, 1) BETWEEN ‘A’ AND ‘G’ ORDER BY tenant_name ASC”) or die(mysql_error());
The magic syntax is LEFT(fieldname, 1) BETWEEN ‘A’ AND ‘G’ . Change the range according to how you would like it to be. LEFT(fieldname,1) meaning taking the first alphabet from fieldname , from the LEFT Â
And that’s it!! You are now able to query records starting from certain alphabet to certain alphabet. Peanut!



Home