希望有人可以提供以下帮助
我有一个包含3列ID,Application,NextVersion
的表这看起来如下
ID1, WindowsXP, ID2,
ID2, Windows7, ID3
ID3, Windows8 , NULL
返回结果表的最佳方法如下。
ID1, WindowsXP, Windows7,
ID2, Windows7, Windows8
ID3, Windows8 , NULL
提前致谢
答案 0 :(得分:0)
你可以进行简单的连接,但是第二个连接,经过一些更改后,你有机会将结果限制为以某个父ID(和所有下降)开头的子项。我不知道你的真实查询会有多复杂
如果你只需要加入然后羞辱你,sql基础知识!
SELECT m.id,
m.name,
y.name
FROM mytable AS m
LEFT JOIN mytable AS y
ON m.parent = y.id
以及更多cpmlocated
CREATE TABLE MyTable
(
id INT,
name VARCHAR(50),
parent INT
)
go
INSERT INTO MyTable
(id,
name,
parent)
VALUES (1, 'WindowsXP', 2),
(2, 'Windows7', 3),
(3, 'Windows8', NULL)
go
WITH x
AS (SELECT *
FROM MyTable)
SELECT m.id,
m.name,
x.name
FROM MyTable as m
LEFT JOIN x
ON m.parent = x.id
go
答案 1 :(得分:0)
试试这个
SELECT
t1.ID,
t1.Application,
t1.NextVersion,
t2.Application AS nextApplicationName
FROM
tbl_name t1
LEFT JOIN
tbl_name t2
ON( t1.NextVersion = t2.ID );
答案 2 :(得分:0)
将yourtable替换为您的表名并执行此
SELECT a.id,
a.[Application],
y.[Application]
FROM yourtable AS a
LEFT JOIN yourtable AS b
ON a.NextVersion = b.id