MySQL IN() for two value/array?

前端 未结 3 2162
失恋的感觉
失恋的感觉 2021-02-18 20:33

I\'m having trouble finding a better way to search MySQL for a pair of values in a table. I have the value pairs in an array, and would like to duplicate the IN() function, but

相关标签:
3条回答
  • 2021-02-18 21:02

    Great answers from @Quassnoi and @KM !!!

    Also, you can get pairs duplicates and select them for post-processing:

    SELECT *
    FROM `foo`
    WHERE (`id_obj` , `Foo_obj`)
    IN (
      SELECT `id_obj` , `Foo_obj`
      FROM `foo`
      GROUP BY `id_obj` , `Foo_obj`
      HAVING count(*) > 1
    )
    
    0 讨论(0)
  • 2021-02-18 21:03
    SELECT  *
    FROM    foo
    WHERE   (column1, column2) IN (('foo', 1), ('bar', 2))
    

    This syntax may be confusing, and it may be more readable to replace it with:

    SELECT  *
    FROM    foo
    WHERE   ROW(column1, column2) IN (ROW('foo', 1), ROW('bar', 2))
    

    I'm used to the former one, though :)

    0 讨论(0)
  • 2021-02-18 21:10

    If you can get your values into a temp table (you only need the two columns) easily and quickly, you can just INNER JOIN your way there. If not, you'll have to use @Quassnoi version.

    0 讨论(0)
提交回复
热议问题