1

Is there any pretty and neat translation of this Postgres query into a MySQL one? Basically, I want to copy values from table_1 into table_2, but in json format, with table_2 not being in first normal form.

INSERT INTO table2(json_meta_data) SELECT row_to_json(row(field_1, field_2)) FROM table_1;

PS: I am not sure about the correctness of this query in Postgres, but I googled and found out Postgres has such functions.

Answer for MySQL 5.7+

Check https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html

8
  • 1
    You may check this - so MySQL can't work for now with json (if only some experimental extension) Commented Aug 29, 2014 at 7:55
  • 1
    No, MySQL has no (built-in) support for JSON. And the gap will even be bigger once Postgres 9.4 with it's enhanced JSON support is released Commented Aug 29, 2014 at 7:57
  • @AlmaDo that's why I wrote: "built-in" Commented Aug 29, 2014 at 7:59
  • Considering that I only want to generate a json string, I think I can write a function. It maybe generate json string by concat() operation. Since I want it to be general, it should be with variable number of arguments. something like to_json(ke1, value1, key2, value2, ...). Can you help me with it? Commented Aug 29, 2014 at 8:20
  • 1
    angry? confused is more like it. If you commence writing your function I wish you the greatest of success. If you get stuck you may ask questions. I am merely trying to warn you that sometimes what looks easy isn't quite as easy when you get into it. Commented Aug 29, 2014 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.