<p>Thanks to @carlcs for pointers managed to get this working with the following:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;limit = NULL;

$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
$params = array(
    ':interest' =&gt; '%"8948"%',
);
$query-&gt;andWhere("field_interests LIKE :interest", $params);
$result = $query-&gt;queryAll() ;
</code></pre>

CC BY-SA 3.0
2016-07-06T15:09:59.473Z
2016-07-06T15:19:43.153Z
2016-07-06T15:19:43.153Z
Lettie
<p>Hi I have a plugin which I am running an sql statement from.</p>

<pre><code>$users = craft()-&gt;db-&gt;createCommand("SELECT craft_users.id as userId, craft_users.firstName, craft_users.lastName, craft_users.email FROM craft_users INNER JOIN craft_content on craft_users.id = craft_content.elementId WHERE craft_content.field_interests LIKE '%\"8948\"%'");
</code></pre>

<p>The query is not returning the expected results from the createCommand function in fact it returns an empty array. But if I run it directly in the database it works perfectly and returns the entries.</p>

<p>I also tried building this with the query builder but it was throwing errors and not finding the user table.</p>

CC BY-SA 3.0
2016-07-06T12:56:07.510Z
Hey Lettie, not answering your question, but here’s another solution to add a `LIKE` condition to a dbCommand http://craftcms.stackexchange.com/questions/10526/get-entry-by-value-from-custom-field
carlcs
CC BY-SA 3.0
2016-07-06T15:07:58.220Z
@carlcs cheers managed to get this working with those pointers.
Lettie
<plugin-development><database><mysql><createcommand>
createCommand sql query not working in plugin
# createCommand sql query not working in plugin

(Asked by: username_0 on 2016-07-06)

Hi I have a plugin which I am running an sql statement from.
$users = craft()->db->createCommand("SELECT craft_users.id as userId, craft_users.firstName, craft_users.lastName, craft_users.email FROM craft_users INNER JOIN craft_content on craft_users.id = craft_content.elementId WHERE craft_content.field_interests LIKE '%\"8948\"%'");

The query is not returning the expected results from the createCommand function in fact it returns an empty array. But if I run it directly in the database it works perfectly and returns the entries.
I also tried building this with the query builder but it was throwing errors and not finding the user table.

**Tags:** plugin-development, database, mysql, createcommand
> **Comments:**
> **username_1**: Hey Lettie, not answering your question, but here’s another solution to add a `LIKE` condition to a dbCommand http://craftcms.stackexchange.com/questions/10526/get-entry-by-value-from-custom-field  
> **username_0**: @carlcs cheers managed to get this working with those pointers.  

---

## 1 answers

### Answer by username_0 on 2016-07-06. Score: 3

Thanks to @carlcs for pointers managed to get this working with the following:
$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->limit = NULL;

$query = craft()->elements->buildElementsQuery($criteria);
$params = array(
    ':interest' => '%"8948"%',
);
$query->andWhere("field_interests LIKE :interest", $params);
$result = $query->queryAll() ;


---

