Answer the question
In order to leave comments, you need to log in
How to parse json string in SQL query and use it for sorting?
Good day comrades! Once again, your ambulance is needed 8)
There is a sql query:
SELECT
`resources`,
`command`,
`timestamp`
FROM
`command`
WHERE
(
(
(`user_id` = 1000196805)
AND (
`command`.`resources` LIKE '%\"14\":%'
)
)
AND (`timestamp` >= 1439856000)
)
AND (`timestamp` <= 1439942400)
ORDER BY
`timestamp` DESC
{"2":61,"14":"24"}, energize, 1439856776.5546
{"2":61,"14":24}, buy, 1439856776.0372
{"2":61,"14":25}, energize, 1439856775.2379
{"2":61,"14":24}, energize, 1439856776.5546
{"2":61,"14":25}, energize, 1439856775.2379
SELECT
DISTINCT
`resources`,
`command`,
`timestamp`,
REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(resources, ',', 2), ',' , -1 ), ':', -1), '"','') as dist
FROM
`command`
WHERE
(
(
(`user_id` = 1000196805)
AND (
`command`.`resources` LIKE '%\"14\":%'
)
)
AND (`timestamp` >= 1439856000)
)
AND (`timestamp` <= 1439942400)
GROUP BY dist
ORDER BY
`timestamp` DESC
Answer the question
In order to leave comments, you need to log in
DISTINCT won't help here, GROUP BY must be used.
SELECT
`resources`,
`command`,
MAX(`timestamp`)
FROM
`command`
WHERE
(
(
(`user_id` = 1000196805)
AND (
`command`.`resources` LIKE '%\"14\":%'
)
)
AND (`timestamp` >= 1439856000)
)
AND (`timestamp` <= 1439942400)
ORDER BY
`timestamp` DESC
GROUP BY
`resources`
first, explain why you need to remove
{"2":61,"14":24}, buy, 1439856776.0372
and not
{"2":61,"14":24}, energize, 1439856776.5546
because the request can return in other sequence!?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question