Answer the question
In order to leave comments, you need to log in
How to build a SQL query to find matches in a JSON string?
Good afternoon, there was such a very interesting task, you need to find records by coincidence, but the string is in JSON format.
When searching by ID, it finds several entries:
{"id":"1000004688","ap_name":"Яблоко","ap_city_id":"","itn":"","kpp":"","okpo":""}
{"id":"1000004688","ap_name":"Апельсин","ap_city_id":"","itn":"","kpp":"","okpo":""}
{"id":"1000004688","ap_name":"Арбуз","ap_city_id":"","itn":"","kpp":"","okpo":""}
{"id":"1000004688","ap_name":"Апельсин","ap_city_id":"","itn":"","kpp":"","okpo":""}
SELECT contragent ,
COUNT(*) OVER() as `total_count` FROM (
SELECT `contragents`.`id`,
CONCAT_WS('', '{\"id\":\"', A.`id`,'\"', ',\"ap_name\":\"', A.`ap_name`,'\"', ',\"ap_city_id\":\"', AA.`name_ru`,'\"', ',\"itn\":\"', A.`itn`,'\"', ',\"kpp\":\"', A.`kpp`,'\"', ',\"okpo\":\"', A.`okpo`,'\"','}') AS `entpr_id`,
CONCAT_WS('', '{\"id\":\"', B.`id`,'\"', ',\"ap_name\":\"', B.`ap_name`,'\"', ',\"ap_city_id\":\"', AB.`name_ru`,'\"', ',\"itn\":\"', B.`itn`,'\"', ',\"kpp\":\"', B.`kpp`,'\"', ',\"okpo\":\"', B.`okpo`,'\"','}') AS `contragent`,
`contragents`.`type`, `contragents`.`ctype`, `contragents`.`resp`, `contragents`.`author`,
`contragents`.`code`, `contragents`.`contract`, `contragents`.`owner`,
COUNT(*) OVER() as `total_count`
FROM `contragents`
LEFT JOIN `companies` A on A.`id` = `contragents`.`entpr_id`
LEFT JOIN `companies` B on B.`id` = `contragents`.`contragent`
LEFT JOIN `cities` AA on AA.`id` = `A`.`ap_city_id`
LEFT JOIN `cities` AB on AB.`id` = `B`.`ap_city_id`
WHERE
`contragents`.`id` IS NOT NULL AND ( `contragents`.`entpr_id` IN (1000004688) )
) AS res
WHERE contragent
LIKE '%Ап%' ORDER BY substring_index(contragent,'\"ap_name\":',-1) LIMIT 50 OFFSET 0;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question