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

mysql - Is “Where IN” with multiple columns defined in Standard SQL?

I'm working on a query like this:

SELECT * FROM requests where (id,langid) IN (SELECT nid,langid FROM node)

My questions are

does this work in mysql and postgresql ? is this something supported by Standard SQL ?

I know is not the best solution and a JOIN would work but I'm not interested in that.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Standard and portable SQL would be EXISTS.. and is semantically the same IN

SELECT *
FROM requests R
WHERE 
    EXISTS (SELECT *
           FROM node n
           WHERE r.id = n.nid AND r.langid = n.langid
           )

The multi-column IN isn't portable to SQL Server or Sybase at least.

Other notes:

  • A JOIN may require a DISTINCT and is not the same as IN or EXISTS.
  • The final option is INTERSECT which is less commonly supported and works like IN/EXISTS
  • IIRC some prehistoric MySQL versions (3.x?) didn't support the correlation for EXISTS

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

...