MySQL multiple columns in IN clause

前端 未结 5 529
既然无缘
既然无缘 2020-12-05 17:32

I have a database with four columns corresponding to the geographical coordinates x,y for the start and end position. The columns are:

  • x0
  • y0
5条回答
  •  暖寄归人
    2020-12-05 18:35

    I do not understand your point. The following query is valid MySQL syntax:

    SELECT *
    FROM my_table
    WHERE (x0, y0, x1, y1) IN ((4, 3, 5, 6), ... ,(9, 3, 2, 1));
    

    I would expect MySQL to use the composite index that you have described. But, if it doesn't you could do:

    SELECT *
    FROM my_table
    WHERE x0 = 4 AND y0 = 3 AND x1 = 5 AND y1 = 6
    UNION ALL
    . . .
    SELECT *
    FROM my_table
    WHERE x0 = 9 AND y0 = 3 AND x1 = 2 AND y1 = 1
    

    The equality comparisons in the WHERE clause will take advantage of an index.

提交回复
热议问题