所以我有一张桌子,看起来像这样:
ID | parentID | name | typeID
---------------------------------------
1 | 2 | thing1 | 1
2 | 4 | region1 | 0
3 | 4 | region2 | 0
4 | NULL | region3 | 0
5 | 3 | foo1 | 2
5 | 3 | foo2 | 2
6 | 3 | bar1 | 3
我需要的是这样的输出:
ID | region | thing | foo | bar
--------------------------------------------
1 | region1 | thing1 | NULL | NULL
2 | region2 | NULL | foo1 | bar1
3 | region2 | NULL | foo2 | NULL
4 | region3 | NULL | NULL | NULL
有了这个
Select ID,
(CASE WHEN [typeID] = 0 THEN [name] END) AS region,
(CASE WHEN [typeID] = 1 THEN [name] END) AS thing,
(CASE WHEN [typeID] = 3 THEN [name] END) AS foo,
(CASE WHEN [typeID] = 5 THEN [name] END) AS bar
FROM [my].[dbo].[foobarthingtable]
where type = 0 OR type = 1 OR type = 3 OR type = 5
我明白了(逻辑上):
ID | region | thing | foo | bar
--------------------------------------------
1 | NULL | thing1 | NULL | NULL
2 | region1 | NULL | NULL | NULL
3 | region2 | NULL | NULL | NULL
4 | region3 | NULL | NULL | NULL
5 | NULL | NULL | foo1 | NULL
6 | NULL | NULL | foo2 | NULL
7 | NULL | NULL | NULL | bar1
如何将这些“foos”、“bars”和“things”与(父)区域放在同一行?JPA 方式 ( CriteriaQuery
) 也很好,因为我必须在 JSF 页面上将其显示为概览。
谢谢