
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Display records on the basis of key-value pairs in MySQL
For this, use JSON_OBJECTAGG(). Let us first create a table −
mysql> create table DemoTable ( Id int, FirstName varchar(100), Age int ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,'John',23); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20,'Carol',21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10,'Sam',24); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20,'Chris',20); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+------+ | Id | FirstName | Age | +------+-----------+------+ | 10 | John | 23 | | 20 | Carol | 21 | | 10 | Sam | 24 | | 20 | Chris | 20 | +------+-----------+------+ 4 rows in set (0.00 sec)
Following is the query using MySQL JSON_OBJECT to display records in key-value pair −
mysql> select Id,JSON_OBJECTAGG(FirstName,Age) from DemoTable GROUP BY Id;
This will produce the following output −
+------+-------------------------------+ | Id | JSON_OBJECTAGG(FirstName,Age) | +------+-------------------------------+ | 10 | {"Sam": 24, "John": 23} | | 20 | {"Carol": 21, "Chris": 20} | +------+-------------------------------+ 2 rows in set (0.07 sec)
- Related Articles
- How to display the day name on the basis of Date of Birth records in MySQL?
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- Search records on the basis of date in MySQL?
- Fetch records on the basis of LastName using MySQL IN()
- Select items based on value first, then order on the basis of date for rest of the records in MySQL
- Select records from a table on the basis of keywords in MySQL
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query to ORDER BY records on the basis of modulus result
- Select and filter the records on month basis in a MySQL table?
- Add a column count in a MySQL query on the basis of last name records?
- Display the student marks in a single column on the basis of subject in MySQL?
- Perform MySQL UPDATE on the basis of DATE value in another column
- Display custom text in a new column on the basis of null values in MySQL?
- Display records from two columns based on comparison in MySQL?
- Count the number of key/value pairs in HybridDictionary in C#

Advertisements