Empty IN clause parameter list in MySQL

后端 未结 9 981
攒了一身酷
攒了一身酷 2020-11-30 08:45

What happens when you do a SQL query where the IN clause is empty?

For example:

SELECT user WHERE id IN ();

Will MySQL

相关标签:
9条回答
  • 2020-11-30 08:58

    If I have an application where I'm building the IN list dynamically, and it might end up empty, what I sometimes do is initialize the list with an impossible value and add to that. E.g. if it's a list of usernames, I'll start with an empty string, since that's not a possible username. If it's an auto_increment ID, I'll use -1 because the actual values are always positive.

    If this isn't feasible because there are no impossible values, you have to use a conditional to decide whether to include AND column IN ($values) expression in the WHERE clause.

    0 讨论(0)
  • 2020-11-30 08:59

    I assume that you still need to run the query when your IN is empty; for example with LEFT JOIN ... ON ... AND IN ().

    As you are already dynamically building the IN in the application layer, I'd handle the empty case there.

    Here's a basic example in PHP

    $condition = 'FALSE' // Could feasibly want TRUE under different circumstances
    if($values){
       $inParams = **dynamically generated IN parameters from $values**;
       $condition = 'IN (' . $inParams . ')';
    }
    
    $sql = 'SELECT * FROM `user` WHERE '.$condition;
    

    If you don't need to run the query with an empty IN, then don't; save yourself a trip to the database!

    N.B. In case you aren't already, I'd build/use a function that goes the long way round and binds in your IN parameters properly, rather than just concatting them raw into the SQL. This will give you some protection against SQL injection if it's used on raw data.

    0 讨论(0)
  • 2020-11-30 08:59

    You can't leave IN operator empty, you must put into it something, NULL for example. But even, in this case, it will be not working as expected, because comparing with NULL always returns NULL (empty). One possible solution is to add a subselect.

    Example:

    SELECT user_id FROM users;
    
    +-----------+
    | user_id   |
    +-----------+
    |      1000 |
    |      1001 |
    |      1002 |
    |      1003 |
    |      1004 |
    |      1005 |
    |      1006 |
    |      1007 |
    |      1008 |
    |      1009 |
    |      1010 |
    +-----------+
    
    SELECT user_id FROM users WHERE user_id NOT IN ();
    ERROR: 1064: You have an error in your SQL syntax; check the manual that 
    corresponds to your MySQL server version for the right syntax to use near ')' at line 1
    
    SELECT user_id FROM users WHERE user_id NOT IN (NULL);
    Empty set (0.0003 sec)    
    
    SELECT user_id FROM users WHERE user_id IN (1001, 1002, 1003) 
    AND user_id NOT IN (SELECT user_id FROM users WHERE user_id IN (NULL));
    +---------+
    | user_id |
    +---------+
    |    1001 |
    |    1002 |
    |    1003 |
    +---------+
    3 rows in set (0.0004 sec)
    

    You cal a little bit modify (shorten) queries, but I think, they also will a little more slowly.

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