MySQL IN clause: max number of arguments

前端 未结 4 2034
难免孤独
难免孤独 2020-12-16 15:00

Say you have the following query:

SELECT * FROM table1 WHERE table1.id IN (1, 2, 3, 4, 5, ..., 999999)

What is a reasonable maximum for the

4条回答
  •  庸人自扰
    2020-12-16 15:49

    You can also have the IN clause take the results of a query, such as:

    SELECT * FROM table1 
    WHERE table1.id IN 
        (
       SELECT id from table2
        )
    

    That way, you don't need to generate a text string with all the possible values.

    In mysql, you should be able to put as many values in the IN clause as you want, only constrained by the value of "max_allowed_packet".

    http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet

提交回复
热议问题