Home > Database > Mysql Tutorial > How to Efficiently Query Database Records Matching Tuples from a List?

How to Efficiently Query Database Records Matching Tuples from a List?

Patricia Arquette
Release: 2024-12-27 14:40:10
Original
942 people have browsed it

How to Efficiently Query Database Records Matching Tuples from a List?

Tuples in SQL "IN" Clause

Q: How can we efficiently query a database table for records matching tuples from a hard-coded list?

A: Standard SQL-92 syntax provides a solution by utilizing the VALUES keyword.

SELECT *
FROM mytable
WHERE (group_id, group_type) IN (
                               VALUES ('1234-567', 2),
                                      ('4321-765', 3),
                                      ('1111-222', 5)
                              );
Copy after login

Implementation Details:

  • Enclose the tuples in single quotes, not double quotes.
  • Add the VALUES keyword before the list of tuples.

Advantages of this Syntax:

  • Eliminates the need for expensive string concatenation and conversion.
  • Supports tuples with multiple values.

Database Compatibility:

  • Standard SQL-92 compliant databases support this syntax.
  • As of SQL Server 2022, Microsoft has not implemented this feature.
  • PostgreSQL and SQLite are examples of databases that support this syntax.

The above is the detailed content of How to Efficiently Query Database Records Matching Tuples from a List?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template