A brief talk about MySQL pivot tables

A brief talk about MySQL pivot tables

I have a product parts table like this:

part

part_id part_type product_id
--------------------------------------
1A1
2 B 1
3A2
4 B 2
5A3
6 B 3

I want a query that returns a table like this:

product_id part_A_id part_B_id
----------------------------------------
1 1 2
2 3 4
3 5 6

In actual implementation, there will be millions of product parts

1 Answer

Unfortunately, MySQL doesn't have a PIVOT function, but you can model it using aggregate functions and a CASE statement. For a dynamic version you'll need to use a prepared statement:

SET @sql = NULL;
SELECT
 GROUP_CONCAT(DISTINCT
  CONCAT(
   'max(case when part_type = ''',part_type,''' then part_id end) AS part_','_id'
  )
 ) INTO @sql
FROM
 parts;
SET @sql = CONCAT('SELECT product_id,',@sql,' 
         FROM parts 
          GROUP BY product_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

If you only have a few columns then you can use the static version:

select product_id,max(case when part_type ='A' then part_id end) as Part_A_Id,max(case when part_type ='B' then part_id end) as Part_B_Id
from parts
group by product_id

Summarize

The above is all the content of MySQL dynamic perspective collected and organized by 123WORDPRESS.COM for you. I hope this article can help you solve the program development problems encountered in MySQL dynamic perspective.

You may also be interested in:
  • Example code of how to implement pivot table in MySQL/MariaDB

<<:  React+TypeScript project construction case explanation

>>:  How to upgrade all Python libraries in Ubuntu 18.04 at once

Recommend

Global call implementation of Vue2.x Picker on mobile terminal

Table of contents What is the Picker component Pr...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

How to understand SELinux under Linux

Table of contents 1. Introduction to SELinux 2. B...

How to install and configure the Apache Web server

Learn how to host your own website on Apache, a r...

Example analysis of mysql variable usage [system variables, user variables]

This article uses examples to illustrate the usag...

Summary of using MySQL isolation columns and prefix indexes

Table of contents Isolate Data Columns Prefix Ind...

Detailed Tutorial on Installing MySQL 5.7 on RedHat 6.5

RedHat6.5 installation MySQL5.7 tutorial sharing,...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

Detailed explanation of Mysql's method of optimizing order by statement

In this article, we will learn about the optimiza...

Tutorial on installing php5, uninstalling php, and installing php7 on centos

First, install PHP5 very simple yum install php T...

Summary of various forms of applying CSS styles in web pages

1. Inline style, placed in <body></body&g...

Solve the problem of garbled data in MySQL database migration

Under the instructions of my leader, I took over ...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...