MySQL conditional query and or usage and priority example analysis

MySQL conditional query and or usage and priority example analysis

This article uses examples to illustrate the usage and priority of MySQL conditional query and or. Share with you for your reference, the details are as follows:

Introduction to mysql and and or

AND and OR can be used to combine two or more conditions in a WHERE clause.

When using the OR keyword:

  • As long as one of the query conditions is met, such records will be queried.
  • If any of these query conditions are not met, such records will be excluded.

When using the and keyword:

  • All conditions must be met and such records will be queried.
  • If any of the conditions are not met, the record will be excluded.

Mysql and and or examples

The table data needed in this example are as follows:

title content category seo_name
PHP Array Analysis of PHP array usage 1 php
mysql distinct mysql distinct example 2 mysql
Java array How to use java array 3 java
php input How to get the value of php input 4 php

(1) AND condition query operator example:

Use AND to display all data with title "php array" and category 1:

SELECT * FROM ar WHERE title='php array' AND category='1'

result:

title content category seo_name
PHP Array Analysis of PHP array usage 1 php

(2) OR conditional operator example

Use OR to display all data with title "java array" or seo_name "php":

SELECT * FROM ar WHERE title='java array' OR seo_name='php'

result:

title content category seo_name
PHP Array Analysis of PHP array usage 1 php
Java array How to use java array 3 java
php input How to get the value of php input 4 php

(3) Combining AND and OR operators

We can also combine AND and OR (using parentheses to form complex expressions):

SELECT * FROM ar WHERE (title='java array' OR category='4')
AND seo_name='php'

result:

title content category seo_name
php input How to get the value of php input 4 php

and and or precedence

You can include any number of and and or operators in the where clause. Without any other symbols, such as parentheses, SQL will first execute the and condition and then the or statement, such as:

select * from table from id=1 or id=2 and price>=10;
/* http://www.manongjc.com/article/1439.html */

By default, this statement executes when id=2 and price is greater than or equal to 10, or when id=1.

If you add brackets:

select * from table from (id=1 or id=2) and price>=10;

This statement executes when id=1 or id=2, and price is greater than or equal to 10.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL learning prerequisites for querying data
  • Essential conditional query statements for MySQL database
  • MySql multi-condition query statement with OR keyword
  • Mysql multi-condition query statement with And keyword
  • How to use case when statement in MySQL to implement multi-condition query
  • A comprehensive summary of common operations of MySQL conditional query statements

<<:  Using JavaScript to implement carousel effects

>>:  Detailed explanation of fuser command usage in Linux

Recommend

How to write the style of CSS3 Tianzi grid list

In many projects, it is necessary to implement th...

Getting Started Tutorial for Beginners ④: How to bind subdirectories

To understand what this means, we must first know ...

Summary of methods for finding and deleting duplicate data in MySQL tables

Sometimes we save a lot of duplicate data in the ...

Installing the ping tool in a container built by Docker

Because the Base images pulled by Docker, such as...

VMware Tools installation and configuration tutorial for Ubuntu 18.04

This article records the installation and configu...

Basic usage examples of Vue named slots

Preface Named slots are bound to elements using t...

What are the advantages of using B+ tree index in MySQL?

Before understanding this problem, let's firs...

Summary of frequently used commands for Linux file operations

0. New operation: mkdir abc #Create a new folder ...

Ubuntu Server 16.04 MySQL 8.0 installation and configuration graphic tutorial

Ubuntu Server 16.04 MySQL 8.0 installation and co...

Exploration and correction of the weird behavior of parseInt() in js

Background: I wonder if you have noticed that if ...

JSONP cross-domain simulation Baidu search

Table of contents 1. What is JSONP 2. JSONP cross...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

Cross-database association query method in MySQL

Business scenario: querying tables in different d...

JavaScript commonly used array deduplication actual combat source code

Array deduplication is usually encountered during...