Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend: How to use SQL query with 'like' keyword?

I am using zend framework. I am using following query in zend and it is working for me perfectly.

$table = $this->getDbTable();
$select = $table->select();
$select->where('name = ?', 'UserName');
$rows = $table->fetchAll($select);

Now I want to create another query in zend with 'like' keyword. In simple SQL it is like that.

SELECT * FROM Users WHERE name LIKE 'U%'

Now how to convert my zend code for above query?

like image 788
Naveed Avatar asked Dec 17 '09 12:12

Naveed


People also ask

How use like with in clause in SQL?

The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can like be combined with and in SQL?

There is no combination of LIKE & IN in SQL, much less in TSQL (SQL Server) or PLSQL (Oracle).

What does SQL GROUP BY do?

The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


1 Answers

Try:

$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', 'UserName%');
$rows = $table->fetchAll($select);

or if UserName is a variable:

$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', $userName.'%');
$rows = $table->fetchAll($select);
like image 165
Justin Swartsel Avatar answered Oct 25 '22 01:10

Justin Swartsel