我怎么能在一个查询中执行此操作

时间:2013-06-06 10:45:01

标签: sql

表名:单词
字段:wId,引理,relatedId
数据:
0苹果1
1馅饼-1

预期结果:
wId = 0
引理=苹果
relatedWord = pie

2 个答案:

答案 0 :(得分:1)

此查询可以执行您想要的操作。据我所知,你的表单词包含relatedld和wld之间的外键。所以这个查询应该满足你的需求:

SELECT t1.wld as wld, 
       t1.lemma as lemma, 
       t2.wld as relatedWorld
FROM words t1 JOIN words t2 ON t1.relatedld = t2.wld;

答案 1 :(得分:1)

您可以参考Here

详情如下:

create table words (wId integer, 
                    lemma varchar(100), 
                    relatedId Integer);
Insert Into words values(0,'apple',1),(1,'pie',-1);

select t1.WID,t1.lemma as Lemma,t2.lemma as relatedWord 
from words t1,words t2
where t1.RelatedID = t2.WID