MySQL condition query and or use instance and priority introduction

Source: Internet
Author: User

MySQL and and or introduction

And and or can combine two or more conditions in a where sub-statement.

When using the OR keyword:

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

When using the AND keyword:

    • All the conditions are met and the records are queried.
    • If any one of these conditions is not met, such records will be excluded.

MySQL and and or instances

The table data you need to use in this example is as follows:

title content category Seo_name
PHP arrays PHP Array Usage Analysis 1 Php
MySQL DISTINCT MySQL distinct instances 2 Mysql
Java array How to use Java array 3 Java
PHP input How PHP input is given a value 4 Php

(1) An and conditional query operator instance:

Use and to display all data with the title "PHP Array" and Category 1:

SELECT * FROM ar WHERE title=‘php数组‘ AND category=‘1‘

Results:

title content category Seo_name
PHP arrays PHP Array Usage Analysis 1 Php

(2) or conditional operator instance

Use or to display all data with the title "Java array" or Seo_name "PHP":

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

Results:

title content category Seo_name
PHP arrays PHP Array Usage Analysis 1 Php
Java array How to use Java array 3 Java
PHP input How PHP input is given a value 4 Php

(3) combining and and OR operators

We can also combine and and or together (using parentheses to form complex expressions):

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

Results:

title content category Seo_name
PHP input How PHP input is given a value 4 Php

And and or precedence

You can include any number of and and or operators in where, and when there are no other symbols, such as parentheses, SQL executes the and condition first, and then executes the or statement, such as:

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

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

If you add parentheses:

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

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

Original address: http://www.manongjc.com/article/1439.html

MySQL condition query and or use instance and priority introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.