我有一个MySQL表,其中包含以下字段和数据;
PartNumber Priority SupName
a1 0 One
a2 0 One
a2 1 Two
a3 0 One
a4 1 Two
a5 2 Three
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个视图,其中具有多行的部分组合成一行,并分成单独的字段,如
理想情况下;
PartNumber Sup1 Sup2 Sup3
a1 One NULL NULL
a2 One Two NULL
a3 One NULL NULL
a4 Two NULL NULL
a5 Three NULL NULL
Run Code Online (Sandbox Code Playgroud)
或者我可以忍受这个
PartNumber Sup1 Sup2 Sup3
a1 One NULL NULL
a2 One Two NULL
a3 One NULL NULL
a4 NULL Two NULL
a5 NULL NULL Three
Run Code Online (Sandbox Code Playgroud)
我将如何构建视图或select语句来实现此目的?
我到目前为止最接近的是;
SELECT PartNumber,
IF(Priority=0, SupName, NULL) …Run Code Online (Sandbox Code Playgroud)