<p>Just before posting the question, I had the good mind to read the <a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand">Yii CDbCommand</a> docs again, and I figured this out myself:</p>

<pre><code>$query-&gt;orWhere(array(
    'like',
    'somecolumn',
    '%foo%'
));
</code></pre>

CC BY-SA 3.0
2015-09-24T22:13:47.773Z
2015-09-24T22:13:47.773Z
Mats Mikkel Rummelhoff
<p>I'm trying to construct a fairly simple LIKE query using the <a href="http://buildwithcraft.com/docs/plugins/database#query-builder" rel="nofollow">Query Builder</a>, and I'm struggling. Here's the SQL that I want:</p>

<pre><code>SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn` LIKE '%foo%') OR (`somecolumn` LIKE '%bar%'))
</code></pre>

<p>I've ran the above query in Sequel, with results.</p>

<p>For the Query Builder, here's what I've got so far:</p>

<pre><code>$query = craft()-&gt;db-&gt;createCommand();
$query-&gt;select('id');
$query-&gt;from('somecustomtable');
$query-&gt;orWhere(array(
    'somecolumn' =&gt; 'LIKE %foo%',
));
$query-&gt;orWhere(array(
    'somecolumn' =&gt; 'LIKE %bar%',
));
</code></pre>

<p>This produces the following query, which obviously doesn't work as intended:</p>

<pre><code>SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn`='LIKE %foo%') OR (`somecolumn`='LIKE %bar%'))
</code></pre>

<p>I've also tried adding the <code>LIKE</code> directive like this:</p>

<pre><code>$query-&gt;orWhere(array(
    'somecolumn LIKE' =&gt; '%bar%',
));
</code></pre>

<p>This produces an error, however.</p>

<plugin-development><database><mysql>
How do I build a LIKE query with DbCommand/Query Builder?
# How do I build a LIKE query with DbCommand/Query Builder?

(Asked by: username_0 on 2015-09-24)

I'm trying to construct a fairly simple LIKE query using the Query Builder, and I'm struggling. Here's the SQL that I want:
SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn` LIKE '%foo%') OR (`somecolumn` LIKE '%bar%'))

I've ran the above query in Sequel, with results.
For the Query Builder, here's what I've got so far:
$query = craft()->db->createCommand();
$query->select('id');
$query->from('somecustomtable');
$query->orWhere(array(
    'somecolumn' => 'LIKE %foo%',
));
$query->orWhere(array(
    'somecolumn' => 'LIKE %bar%',
));

This produces the following query, which obviously doesn't work as intended:
SELECT `id` FROM `craft_somecustomtable` WHERE ((`somecolumn`='LIKE %foo%') OR (`somecolumn`='LIKE %bar%'))

I've also tried adding the LIKE directive like this:
$query->orWhere(array(
    'somecolumn LIKE' => '%bar%',
));

This produces an error, however.

**Tags:** plugin-development, database, mysql

---

## 1 answers

### Answer by username_0 on 2015-09-24. Score: 7 | Accepted answer

Just before posting the question, I had the good mind to read the Yii CDbCommand docs again, and I figured this out myself:
$query->orWhere(array(
    'like',
    'somecolumn',
    '%foo%'
));


---

