Home > database >  How to implement the table where the query
How to implement the table where the query

Time:09-27

Such as table A is responsible for storing user information
Table B store user badge information
Two tables for UID is
Each user may have more than one badge

How to do a query in mysql statement for multiple user information, user information to nested badge information

Also please comment

Expectations get the json data
[{
UserName: "zhang",
UID: 1,
The badge: [{badgeLevel badgeName: "membership" : "1"}]
},
{
UserName: "bill,"
UID: 1,
Badge: [{badgeLevel badgeName: "membership" : "1"}, {badgeLevel badgeName: "association" : "1"}]
}, {
UserName: "detective",
UID: 1,
The badge: null
}]

CodePudding user response:

The SELECT b1. The userName, b2 badgeName, b2. BadgeLevel from user1 b1 INNER JOIN badge on b1, b2 uid
uid=b2.
This can query the basic information of each user and badge information, and then you traverse the assembly into a json format you want to

CodePudding user response:

Two table LianZha join try

CodePudding user response:

Background processing code, use the Iterator traversal, using a TreeMap additional List

CodePudding user response:

reference cw_9312 reply: 3/f
code processing, use the Iterator traversal, use TreeMap append a List can


Background processing such code efficiency is low, an SQL processing directly shows that there is not very convenient
  • Related