21 AND name LIKE '%John%'."/> 21 AND name LIKE '%John%'.">

Home >Database >SQL >Usage of and in sql

Usage of and in sql

下次还敢
下次还敢Original
2024-05-08 11:18:18953browse

The AND operator is used to combine multiple conditions and returns TRUE only if all conditions are TRUE. Syntax: WHERE condition1 AND condition2 AND ..., where condition is the condition that evaluates to TRUE or FALSE. For example, to get customers whose age is greater than 21 and whose name contains "John", the query is: SELECT * FROM customers WHERE age > 21 AND name LIKE '%John%'.

Usage of and in sql

Usage of AND operator in SQL

AND operator is a logic used to combine multiple conditions in SQL operator. It returns TRUE if and only if all specified conditions are TRUE.

Grammar:

<code>WHERE condition1 AND condition2 AND ...</code>

Among them:

  • condition1, condition2, etc. are evaluations A condition that is TRUE or FALSE.

Usage example:

Consider the following table structure:

<code>CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  age INT
);</code>

The following query obtains all those whose age is greater than 21 and whose name contains "John" Customer:

<code>SELECT * FROM customers
WHERE age > 21 AND name LIKE '%John%';</code>

In this query:

  • age > 21 The condition checks if the age of the customer is greater than 21.
  • name LIKE '%John%' The condition checks whether the customer's name contains the substring "John".
  • The AND operator combines these two conditions, ensuring that only customers who meet both conditions are returned.

Note:

  • The AND operator has a higher priority than the OR operator.
  • Brackets can be used to group conditions and control the precedence of operators.
  • The AND operator is different from the && operator, which is used for logical evaluation in programming languages ​​such as Python.

The above is the detailed content of Usage of and in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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