0

我有下表

    ID    p_name     p_value
   ----   ------     --------------
    1      name       index.htm
    1      path       c:\web\site1
    1      size       400
    1      date       2011-03-12
    1      note       'test site'
    2      name       login.htm
    2      path       c:\web\site1
    2      size       500
    ...    ...        ...
    ...    ...        ...

我需要让它看起来像这样

    ID   Name         Path
   ---   ----------   -------------
    1     index.htm    c:\web\site1
    2     login.htm    c:\web\site1
    ...   ...          ...

我正在阅读其他几篇文章,但我不知道如何在我的示例中应用它!我只需要主要的“名称”和“路径”,我可以在需要时添加其他!

4

2 回答 2

1
select t1.id, t1.p_value name, t2.p_value path
  from mytable t1, mytable t2
 where t1.id = t2.id
   and t1.p_name = 'name'
   and t2.p_name = 'path'
于 2012-05-15T07:26:12.500 回答
1

最简单的方法是将其排除,因为对于给定的 ID,您似乎有有限数量的 p_name。

尝试 2 - 修改

Select ID, T1.P_Name as Name, T2.P_value as Path
FROM YourTableName T1
INNER JOIN yourTableName T2 
  ON T1.ID = T2.ID 
  and T1.P_Name = 'name' 
  and T2.P_name = 'path'

尝试 1 - 失误

SELECT Id,
CASE p_name when 'name' THEN P_value end as Name,
CASE p_name when 'path' THEN p_value end as Path
FROM yourTableNameHere
于 2012-05-15T06:38:37.983 回答