Home >Database >Mysql Tutorial >How Can I Efficiently Compare Tuples in SQL's `IN` Clause?
When working with complex WHERE clauses, it's often necessary to compare a field or set of fields against a list of values. In the case of comparing tuples, there are several options available.
The IN Clause with Hardcoded Values
One approach is to use hardcoded values within the IN clause. However, this can be inefficient for large tables, as each record requires string concatenation and conversion.
Using VALUE Clauses (Standard SQL)
A more efficient option for Standard SQL-92 databases is to use VALUES clauses within the IN operator. This involves enclosing the tuple list in parentheses and adding the VALUES keyword.
SELECT * FROM mytable WHERE (group_id, group_type) IN ( VALUES ('1234-567', 2), ('4321-765', 3), ('1111-222', 5) );
Database Compatibility
It's important to note that this syntax is not supported by all SQL products. As of SQL Server 2022, this feature is considered unplanned. PostgreSQL and SQLite, however, do support this syntax.
The above is the detailed content of How Can I Efficiently Compare Tuples in SQL's `IN` Clause?. For more information, please follow other related articles on the PHP Chinese website!