user3205047
user3205047

Reputation: 199

Filter column value by the first character?

This is what I've done so far.

$filtered_results = array();

$result = $this->db
               ->select('name')
               ->from('user')
               ->get()
               ->result_array();    

foreach($result as $name)
{
    if($name[0]=='a')
    {
        filtered_results[] = $name;
    }
}

The above code logically wastes too much time processing all the results if my table contains tons of rows. So, the other way around is to directly retrieve only results with 'a' as their $name[0];. Is this possible?

->where('name'.[0],'a')

Upvotes: 3

Views: 13774

Answers (1)

Orel Eraki
Orel Eraki

Reputation: 12196

This will be a start:

Example1:

SELECT `name` FROM `user` where `name` like 'a%'

Example2: (Marc B)

SELECT `name` FROM `user` WHERE LEFT(`name`, 1) = 'a'

Upvotes: 5

Related Questions