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
527 views
in Technique[技术] by (71.8m points)

sql - How do I compare two columns in the same table?

I would like to compare two columns in the same table. I want to be able to return all rows where the two columns have the same value.

I am looking for something like SELECT * FROM FOO WHERE C1 = C4.

Therefore in the example below I would return only the first row:

C1    || C2  || C3  || C4
--------------------------
1     || a   || b   || 1
2     || a   || b   || 4
3     || b   || d   || 2
4     || b   || d   || 2

If it matters, I am using SQLite (more specifically WebSQL).

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

SELECT * FROM FOO WHERE C1 = C4 should work. Does it not?

If not, are they the same data type and length? You may need to convert.

I don't know about WebSql, but I've seen some db systems that refuse to match if one is a varchar(5) and the other is a varchar(10) even though they hold the same value. In those systems you have to use something like

 Convert(varchar, 10, FieldName)

to get a match.


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

...