适用于初学者的“ SQL行到列”帮助

时间:2019-02-21 08:25:40

标签: sql oracle

我在这里学习。我需要用于以下情况的最佳实践。没有SQL经验...

表格:

Id   Second_id   Wanted_rows
10   61          Blue
10   65          999-JHD
10   70          Gasoline
11   61          Red
11   65          786-FDX
11   70          Disel

我需要得到这个:

Car_id   Color   Engine     Plate
10       Blue    Gasoline   999-JHD
11       Red     Disel      786-FDX

2 个答案:

答案 0 :(得分:0)

尝试类似的方法(我已经在记事本中将其删除)。

SELECT Car_id as a.id, Color As a.Wanted_rows, Engine As b.Wanted_rows,  Plate As b.Plate
FROM TableA 
INNER JOIN TableB ON TableA

然后从那里查看小组等等

答案 1 :(得分:0)

您可以尝试以下方法:

select t1.id, t1.wanted_rows as color, t2.wanted_rows as engine, t3.wanted_rows as Plate  
  from data t1
    inner join data t2 on t2.id = t1.id and t2.second_id = 70
    inner join data t3 on t3.id = t1.id and t3.second_id = 65
  where  t1.second_id = 61
order by t1.id

select t1.id, t1.wanted_rows as color,
(select wanted_rows from data where id = t1.id and second_id = 70) as engine,
(select wanted_rows from data where id = t1.id and second_id = 65) as plate
  from data t1 
  where  t1.second_id = 61 
order by t1.id

必须在其中放置表名称的数据。