Before I write a Python script, I want to see if only MySQL can create the result.
I have a list of products:
PID Product ----------- 1 AAA 2 ABC 3 BAC 4 CAB 5 CBA
I have a list of companies ordering these products several times:
CID PID ------- 1 1 2 3 1 5 3 2 1 1 2 3
Desired Result:
CID AAA ABC BAC CAB CAB CBA --------------------------- 1 YY 2 Y 3 Y
How will I do this in Python?
- Create a temporary table with columns (CID AAA ABC BAC CAB CAB CBA)
- Run 2 cycles and update the desired table when the desired column matches.
It's just interesting to know if there is only a MySQL solution.
ps: This is just a sample, and the actual problem has several 100 products and several thousand companies. I created a temporary table for 100 products by transposing in Excel and converting it to a MySQL table.
The following is the approach I finally resorted to. Thanks for the feedback.
Use the generated request in the format suggested by the nickname.
select cid, max(case when pid = 1 then 'Y' else '' end) as AAA, max(case when pid = 2 then 'Y' else '' end) as ABC, max(case when pid = 3 then 'Y' else '' end) as BAC, max(case when pid = 4 then 'Y' else '' end) as CAB, max(case when pid = 5 then 'Y' else '' end) as CBA from companies group by cid
source share