how to search value in json array mysql - php

I saved some array value via JSON encode to some table recode.here is JSON value
category_id ---- ["2","3"]
and I need search query to search category_id.someone can explain to me how I do this
$search=$_GET['sq'];
$sql="SELECT * FROM table WHERE category_id =".$search;

Here how I got the correct query using JSON_SEARCH method
SELECT * FROMtableWHERE JSON_SEARCH(category_id, 'all', '%1%') IS NOT NULL

Related

Query specific text inside json text data in MYSQL

I'd like to query from the reviewed_by table below where the "company" is "AAA" and "review" is "Need Review"
Here's mysql table :
+-----------+
| DATA_TYPE |
+-----------+
| text |
+-----------+
+-------------------------+
| reviewed_by |
+-------------------------+
|[{"company":"AAA","review":"OK","reviewed_at":"2021-01-26 08:59:26"}]|
|[{"company":"BBB","review":"OK","reviewed_at":"2021-01-26 08:59:26"}]|
|[{"company":"AAA","review":"Need Review","reviewed_at":"N\/A"}]|
+-------------------------+
Here's the #1 query i've tried :
SELECT * FROM `t_transaction`
WHERE `reviewed_by`
LIKE '%`"company":"AAA","review":"Need Review"`%'
Here's the #2 query i've tried :
SELECT * FROM `t_transaction`
WHERE `reviewed_by`
LIKE '%"company":"AAA","review":"Need Review"%'
ci3 query :
$like = ['reviewed_by','"company":"AAA","review":"Need Review"'];
$this->db->select('*')
->from('t_transacion')
->group_by('id')
->like($like[0],$like[1]);
The result i've got from those 2 queries was nothing,
How can i do this type of query (and also if using codeigniter 3) ?
MySql has some functions that allow you to do search over a json field. See documentation.
The reviewed_by column is a json array and you want to seach the first element of that array. Using the function JSON_EXTRACT you can extract data from the json field. In your case to get the json in the first position in the array so we execute JSON_EXTRACT(reviewed_by, '$[0]') which will return {"company":"...","review":"..","reviewed_at":"..."}. From the returned json we can call again the JSON_EXTRACT function to get a value given a key. If we select JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.company") this will return the company value from inside the json.
There are different ways to select what you want. I will give you two option and they have pros and cons. Take a look at this stackoverflow.
First approach using the where clause:
SELECT reviewed_by
FROM t_transaction
WHERE JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.company") = "AAA"
AND JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.review") = "Need Review";
Second approach using the having clause:
SELECT JSON_EXTRACT(reviewed_by, '$[0]') AS json
FROM t_transaction
HAVING json -> "$.company" = "AAA"
AND json -> "$.review" = "Need Review";

Mysql Json - Fetch perticular object from json array on the basis of given id

I have a field of json type named 'user' in 'users' table containing following data
[{"uid":"213", "name":"Tim", "email":"user1#example.com"}, {"uid":"214", "name":"Cook", "email":"user2#example.com"}, {"uid":"215", "name":"Peter", "email":"user3#example.com"}, {"uid":"216", "name":"Jhon", "email":"user4#example.com"}]
I want to fetch user object of given uid
used this Query
SELECT * FROM users WHERE json_extract(`user`, "$[0].uid") = "213"
but it returns entire array in user field
result that i want is - {"uid":"213", "name":"Tim", "email":"user1#example.com"}
tried this also
SELECT * FROM users WHERE json_extract(`user`, "$[*].uid") = "213"`
but it returns NULL
How to find particular object of given uid?
You need to provide JSON_EXTRACT(user, "$[0].uid")" in SELECT too.
Try
SELECT *, JSON_EXTRACT(`user`, "$[0].uid") FROM users WHERE JSON_EXTRACT(`user`, "$[0].uid") = "213"
Referance

How to cast string to list in PHP or MySQL

I have a list of ids I want to fetch data from a Database.
$ids = "1,2,3,4";
Here is my query
$this->Execute("SELECT * FROM table WHERE id in (?)",array($ids));
This only gives my result where id=1. How do I fetch data where id is from 1 to 4?
Use FIND_IN_SET since $ids is a string
$this->Execute("SELECT * FROM table WHERE FIND_IN_SET(id, ?)", array($ids));

Selecting using MATCH-AGAINST and IN in MySQL query?

I have the following query in PHP:
$titlematch = mysql_query("SELECT `item_id` FROM `items` WHERE `item_id` IN ($matches_s) AND MATCH (`eng_name`) AGAINST ('$query') IN NATURAL LANGUAGE MODE AS score;");
$titlematch2 = mysql_fetch_assoc($titlematch);
In plain words, I want to select the item_id from the table items where
the item_id value can be found within the array $matches_s, and
the string stored in eng_name can be found within the string $query
and then output a 2-dimensional array, where the second-level array contains the item_id and score (relevance) elements.
I have tried switching the order of which criteria comes first, or using brackets. Here are the results I get:
var_dump($titlematch); // returns bool(false)
var_dump($titlematch2); // returns NULL
Question is 'What am I doing wrong? Do I have to write this differently to other AND statements?

How to perform a SQL query that orders by a column of type JSON?

I am using a POSTGRESQL database and I need to order by a column that is of type JSON. This is the error I get:
ERROR: could not identify an ordering operator for type json
LINE 1: SELECT * FROM "field" "t" ORDER BY "t"."brand" LIMIT 10
Is it possible to order by JSON data type? Any suggestion of a work around?
Table field:
field_id INT
name TEXT
type INT
brand JSON
Here is my code:
$sql = 'SELECT * FROM "field" "t" ORDER BY "t"."brand"';
$data = Yii::app()->db->createCommand($sql);
$result = $data->queryAll();
You'd need json accessors for this. As of PG 9.2, none are built-in:
http://www.postgresql.org/docs/9.2/static/functions-json.html
PG 9.3 will feature those that you'd need:
http://www.postgresql.org/docs/9.3/static/functions-json.html
In the meanwhile, you could write an accessor method in e.g. plperl, or install an extension:
https://github.com/theirix/json_accessors

Categories