In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you the "sample analysis of MySQL PivotTable", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of MySQL PivotTable".
I have a product parts list like this:
Part
Part_id part_type product_id--1 A 12 B 13 A 24 B 25 A 36 B 3
I want a query that returns the following table:
Product_id part_A_id part_B_id--1 1 22 3 43 5 6
In practice, there will be millions of product components
Best answer
Unfortunately, MySQL does not have PIVOT functionality, but you can model it using aggregate functions and CASE statements. For dynamic versions, you need to use prepared statements:
SET @ sql = NULL;SELECT GROUP_CONCAT (DISTINCT CONCAT ('max (case when part_type ='', part_type,''' then part_id end) AS part_','_id')) INTO @ sqlFROM 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 have only a few columns, 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_Idfrom partsgroup by product_id is all the content of the article "sample Analysis of MySQL PivotTable". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.