SQL Advanced Chapter

Source: Internet
Author: User

1. Select top

In MySQL:

SELECT * from persons limit 5;

Equivalent to the following in Oracle:

SELECT * FROM Persons <=5;

Often used in the page turn:

SELECT * from persons limit 5, 10;

2. Like

The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.

SELECT * from Websites WHERE name is like ' g% '; by G (%g is the end of G, fuzzy query is%g%)

% is a wildcard

wildcard characters Description
% Replace 0 or more characters
_ Replace one character
[charlist] Any single character of the word columns
[^charlist]
Or
[! charlist]
Any single character that is not in the word columns

The following SQL statement chooses name to start with "G", then an arbitrary character, then "O", then an arbitrary character, then "le" for all sites:

SELECT * from Websites WHERE name is like ' G_o_le ';

MySQL uses the REGEXP or not REGEXP operator (or rlike and not rlike) to manipulate regular expressions.

The following SQL statement selects all Web sites that have the name start with "G", "F", or "s":

SELECT * from Websites WHERE name REGEXP ' ^[gfs] ';

The following SQL statement selects a Web site whose name begins with a to H letter:

SELECT * from Websites WHERE name REGEXP ' ^[a-h] ';

The following SQL statement selects a Web site whose name does not start with a to H letter:

SELECT * from Websites WHERE name REGEXP ' ^[^a-h] ';

3, in

The in operator allows you to specify multiple values in the WHERE clause

The following SQL statement selects all sites with the name "Google" or "Rookie Tutorial":

SELECT * from Websites WHERE name in (' Google ', ' rookie Tutorial ');

4, between

The between operator is used to select values in the range of data between two values

The following SQL statement selects all Web sites with Alexa between 1 and 20:

SELECT * from Websites WHERE Alexa between 1 and;

To display sites that are not within the scope of the above instances, use not between:

The following SQL statement selects the Alexa between 1 and 20 but country is not a USA and IND for all sites:

EM style= "line-height:1.5;" > SELECT * from websites  WHERE (Alexa between 1 and)   and not country in (' USA ', ' IND ');

Example of a between operator with literal values

The following SQL statement selects all sites with name starting with the letters between ' A ' and ' H ':

SELECT * from Websites WHERE name between ' A ' and ' H ';

The following SQL statement selects all access records from date between ' 2016-05-10 ' and ' 2016-05-14 ':

SELECT * from Access_log WHERE date between ' 2016-05-10 ' and ' 2016-05-14 ';

Please note that in different databases, the between operator produces different results!
In some databases, between selects a field between two values but does not include two test values.
In some databases, between selects a field between two values and includes two test values.
In some databases, between selects a field between two values and includes the first Test value but excludes the last Test value.

Therefore, please check how your database handles the between operator!

SQL Advanced Chapter

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.