php - Yii2 - active record query search for a number in a string field -


i have string field contains json string in table. lets name tbl_table

lets has these 3 records in it:

id    json ---------------- 1     [123,45,1] 2     [23,4,5] 3     [7,8,9] 

now want fetch records contain number in json field (using active records in yii2), so:

$res = table::find()->where(['like','json','23'])->all(); 

this query returns these records:

id    json ---------------- 1     [123,45,1] 2     [23,4,5] 

this not wrong result want return record id=2. how can this?

and ['like','json','5'] or ['like','json','4'] , on? numbers similar want exact number match.

and question want last question answer change in part ['like','json',[4,5,6]]. in other words want pass array operator , automatically make or operation each element. somehow possible? if not how can iterate on array , make multiple or_like conditions last question functionality without multi query executions ?

use expression use mysql functions , use json_contains function

use yii\db\expression; $expression = new expression('json_contains(fied, value)'); $now = (new \yii\db\query)->select($expression);   

p.s json_contains begins version mysql 5.7

refs https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html http://www.yiiframework.com/doc-2.0/yii-db-expression.html


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -