Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

sql server - Check if column value exists in another column in SQL

I need a SQL query that compares two values and returns an ID.

I have this table:

ID  Calling_ID  Called_ID
1   27          10
2   15          20
3   80          90
4   90          88
5   60          30
6   88          40
7   15          60
8   30          40
9   27          95
10  40          30

How do I check if each value in the Calling_ID column exists in the Called_ID column and then return the ID? The above data would return 88, 30, 40.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work:

SELECT ID FROM [TableName]
WHERE Calling_ID IN
(
SELECT Called_ID FROM [TableName]
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...