执行此查询的最佳方法是什么.我有下表
列表的mytable
x y
1 a
2 b
3 c
Run Code Online (Sandbox Code Playgroud)
我想(在伪sql中)
select x as x1 ,x as x2, x as x3 from mytable where ????
Run Code Online (Sandbox Code Playgroud)
什么时候
x1 is x where y=a
x2 is x where y=b
x3 is x where y=c
Run Code Online (Sandbox Code Playgroud)
所以我想结果
1, 2, 3
Run Code Online (Sandbox Code Playgroud)
我目前正在使用cte和一个非常大的数据集,我试图减少查询时间,是否总是需要进行3次表扫描?
Sil*_*ght 13
您应该使用3个查询.在自连接时使用适当的索引会更快.此外,它将更具可读性.
如果你想要一个查询调用,它可能是这个:)
SELECT
(SELECT x FROM table WHERE y=1) AS x1,
(SELECT x FROM table WHERE y=2) AS x2,
(SELECT x FROM table WHERE y=3) AS x3
Run Code Online (Sandbox Code Playgroud)
我会这样做的:
SELECT
tableRowA.x as x1
tableRowB.x as x2
tableRowC.x as x3
FROM
table as tableRowA,
table as tableRowB,
table as tableRowC
WHERE
tableRowA.y = 1
tableRowB.y = 2
tableRowC.y = 3
Run Code Online (Sandbox Code Playgroud)
更容易理解,如果每行需要多列,请提取更多信息