Get the App
SLTechnology News&Howtos  ›  Database  › 

How to assemble query results in mysql

Shulou Source: shulou.com Published: 2022-05-31 19:34:00 10月03日 Update

Mysql how to splice query results, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

SELECT

CONCAT ("["

GROUP_CONCAT (

CONCAT ("{Name:'", `NAME`, "'")

CONCAT ("{price:'", `PRICE`, "'")

CONCAT (", id:'", `ID`), "'}")

, "]")

AS json FROM

Goods

Table data

Id name price

1 big tree

two

Result

[{Name:' Tree'{price:'2',id:'1'}]

Note that concat here is a string concatenation function and group_concat is an aggregate function.

When there is more than one data

Table data

Id name price

1 big tree

two

2 ha ha

three

Result

[{Name:' tree'{price:'2',id:'1'}, {Name:' '{price:'3',id:'2'}]

The above is a brief introduction and the following is an in-depth use: using the combination of groupconcat and concat to complete the task of splicing into json

Table data

Goods

Id name price

1 big tree

two

2 ha ha

three

Order

Goodid num

one

two

one

one

two

two

SELECT

CONCAT ('[', GROUP_CONCAT ('{', `ID`,',', `josn1`,'}'),']') AS result

FROM goods JOIN (SELECT `goodid`

GROUP_CONCAT (

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Big tree data result function help query clear task content multiple character string for this that is article novice time more knowledge industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Shulou Tech Info OPPO Reno MariaDB