Home > Database > SQL > body text

Usage of wherein in sql

下次还敢
Release: 2024-05-02 00:42:13
Original
662 people have browsed it

WHERE IN in SQL is used to check whether a column contains a specific set of values. Syntax: SELECT column_name FROM table_name WHERE column_name IN (value1, value2, ..., valueN);. It checks if each value in the column matches any value in the given list of values ​​and returns the row if it matches, otherwise ignores the row.

Usage of wherein in sql

Usage of WHERE IN in SQL

The WHERE IN operator is used to check whether a column contains a specific set of value. It uses the following syntax:

<code>SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, ..., valueN);</code>
Copy after login

How it works

The WHERE IN operator checks whether each value in a column matches any value in the given list of values. If there is a match, the corresponding row is returned; if there is no match, the row is ignored.

Example

Suppose we have a table called "users" with the following columns:

  • id
  • name
  • email

To find the email address of a user named "John" or "Mary", you can use the following query:

<code>SELECT email
FROM users
WHERE name IN ('John', 'Mary');</code>
Copy after login

This will return rows with the name "John" or "Mary" as follows:

<code>email
-----------------
john@example.com
mary@example.com</code>
Copy after login

Note:

  • Lists of values ​​can use single quotes (' ) or enclosed in double quotes (").
  • The IN operator can also be used with subqueries.
  • The WHERE IN operator may be less efficient for large lists of values. Consider using JOIN or EXISTS operator as an alternative.

The above is the detailed content of Usage of wherein in sql. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!