简体   繁体   中英

Select rows where (field1, field2, field3) in set (val11,val21,val31),…,(val11,val21,val31)

I'm using mysql. I need to select rows from table (with fields id,field1,field2,field3 ) where (field1,field2,field3) in set of vectors (v11,v21,v31),(v12,v22,v32),...,(v1N,v2N,v3N) . Is it possible to do it with one query? If yes, what fields do I need to index to speed up the query? And is this query faster/slower then N serial simple select queries?

Thank you in advance!

You can do a multi-column IN comparison like so:

SELECT id,
       field1,
       field2,
       field3
FROM   tbl
WHERE  (field1,field2,field3) IN (
           (v11,v21,v31),
           (v12,v22,v32),
           (v1n,v2n,v3n)
       )

And it's essentially saying:

Where

(field1 = v11 AND field2 = v21 AND field3 = v31) OR
(field1 = v12 AND field2 = v22 AND field3 = v32) OR
...                                              OR
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM