Extract column text from a table

Asked

Viewed 299 times

2

I have the following table structure:

inserir a descrição da imagem aqui

The field metadata is like text type, but I believe it is actually a JSON.

When I do:

SELECT metadata FROM  maxpay.mp_pay_orders;

The result is:

{"idtransactions":122882,"transação":122882}

I need to extract the idtransactions of this field, in a way that I can consult you with a IN. Example:

SELECT idtransactions from tabela1 where idtransactions in
    (select metadata??? from maxpay.mp_pay_orders)

The Mysql version of the server is 5.6. From the answers, I have seen that it is not JSON, since it is only available from version 5.7.8. But I can’t extract that information from that text in any way.

  • What language will you work with? I say this because each language has its own way of working with JSON.

  • Database language do you speak? Why do I need to extract this within the database, not in the application. Mysql

  • Programming language, type, PHP, Java, Python, C# etc...

  • As I said, my problem is not in the application. I want to extract information from the database, by the database itself. I don’t even know what language is used in the application.

  • What version of your Mysql?

  • My version is 5.6, and I don’t think I can change this version because it’s the same as the server.

Show 1 more comment

1 answer

2


From Mysql 5.7.8 you can use columns of the JSON type.

Then you could use one of the available functions to make his SELECT, as the function JSON_EXTRACT for example:

SELECT
    *,
    JSON_EXTRACT('metadata', '$.idtransactions')
FROM maxpay.mp_pay_orders;

If your server does not support JSON columns you can use one REGEX to extract information from the string. Ex.:

SELECT
    *,
    metadata REGEXP '"idtransactions":\\d+'
FROM maxpay.mp_pay_orders as po
INNER JOIN maxpay.transactions as t
    ON po.metadata REGEXP CONCAT('"idtransactions":', t.id, '\\D')
;

In the above example the regex is being used to do the INNER JOIN with the table maxpay.transactions using the field idtransactions of a JSON.

I use the CONCAT to create a regex for each comparison made with the table transactions. The \\D at the end is for a id as 123 do not also marry 12300. This way we limit only to the exact id

  • When I try to run, it makes that mistake: SQL Error [1305] [42000]: FUNCTION mm.JSON_EXTRACT does not exist

  • Mysql 5.7.8 or more?

  • My server version is 5.6.

  • JSON columns are only available from version 5.7.8 onwards, as mentioned in the reply.

  • This is the question. I have already consulted these functions and tried all that I thought possible, but always gives this error that the function does not exist. I tried to upgrade my Workbench to 8.0, but since the server version is 5.6, the same error always occurs.

  • I understood that it is only available from version 5.7.8, but in the case of table structure, I need to extract this information. Is there any other way?

  • I will update the question to use REGEX but it is a gambiarra and is not performatic

  • No problem, I just need to extract that information somehow. :)

  • Updated issue using REGEXP in JOIN

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.