search
HomeDatabaseMysql TutorialComprehensive exposure to SQL syntax (5)

Comprehensive exposure to SQL syntax (5)

Dec 24, 2016 pm 05:47 PM
sql syntax

BETWEEN...AND operator

determines whether a certain value is within a specific range. This operator can only be used in SQL statements.


exPR[Not]BETWEEN value1 AND value2
expr
Specifies the combination of fields and expressions to be calculated.
value1,value2
the value range specified.
For example:
If you want to query all employees between the ages of 25-30 from the employee table, you can use the following program.
SELECT name, age BETWEEN 25 AND 30
FROM staff table;



LIKE operand

Used to combine a string with another specific character Compare string patterns (pattern) and filter out records that match the string pattern.


expression LIKE "pattern"
expression
is used in WHERE conditional clauses and SQL expressions.
pattern
String pattern used for comparison.
For example:
If you want to query all the surnames starting with "Li", you can use the following formula.
Like "李*"
Multiple examples of LIKE operands:
1, multiple characters:
(1)"a*a"
Filterable: "aa"," aBa", "aBBBa", cannot be filtered: "aBC"
(2)"*ab*"
can be filtered: "abc", "AABB", "Xab", cannot be filtered: "aZb"," bac"
2, special characters:
"a"*"a"
can be filtered: "a*a", cannot be filtered: "aaa"
3, single character:
" a?a"
can filter: "aaa", "a3a", "aBa", cannot filter: "aBBBa"
4, single number:
"a#a"
can filter: "a0a", "a1a", "a2a", cannot filter: "aaa", "a10a"
5, character range:
""a-z""
can filter: "f", "p ","j", cannot filter: "2","&"
6. Specify the range beyond characters:
""!a-z""
7. Specify non-digits:
"" !0-9””
Can filter: "A", "a", "&", "~", cannot filter: "0", "1", "9"
8, combined structure :
"a"!b-m"#"
Can filter: "An9", "az0", "a99", cannot filter: "abc", "aj0"



SQL numeric function

1. AVG: arithmetic mean

AVG(expr)

expr

Field name or expression.


For example:

If you want to calculate the average height of employees whose height exceeds 165 cm, you can use the following SQL statement to complete.

 SELECT Avg(height)

 AS average height

 FROM staff table WHERE height>165;

 2. COUNT: Calculate the number of records

 COUNT(expr)

 expr


 Field name or expression.

For example:

If you want to count the number of employees in the business department and query the names of the employees, you can use the following program.

SELECT Count (name) AS Staff name

FROM Staff table

WHERE Department name = 'Business Department';

3. FIRST and LAST: Returns the first and last data of a field.

 FIRST(expr)


 LAST(expr)

 expr

 Field name or expression.

For example:

If you want to find the first data in the product quantity field and the last data in the product price field, you can use the following query method.

SELECT FIRST (item quantity), LAST (item price)


FROM order form

4. MAX, and MIN: return the maximum value and the maximum value of a field minimum value.

Usage is the same as FIRST and LAST.

5. SUM: Returns the sum of a specific field or operation.

 SUM(expr)

 expr

 Field name or expression.


For example:

To calculate the total price of the goods, use the following procedure.


SELECT

Sum (unit price * quantity of goods)

AS total price of goods FROM order form

Multi-layer SQL query

As the name suggests, the purpose of multi-layer SQL query is: "One SQL statement can contain another SQL query statement, forming an internal nested query type."

comparison[ANY|ALL|SOME] (sqlstatement)
expression[NOT]IN (sqlstatement)
[NOT]EXISTS(sqlstatement)
comparison
The operation of comparing an expression with the result of the inner query.
expression
Expression for searching the results of the inner query.
sqlstatement
is a SQL query composed of a SELECT statement, and the statement must be enclosed in ().



For example:

We first query all the units from the order form, and then compare the units in the product table one by one to query all records with unit prices higher than those in the order form.

SELECT * FROM product form

WHERE unit price>ANY (SELECT unit price FROM order form WHERE discount>=.25);

The above is full contact with SQL Grammar (5) content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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
What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

Explain the differences between InnoDB and MyISAM storage engines.Explain the differences between InnoDB and MyISAM storage engines.Apr 27, 2025 am 12:20 AM

InnoDB is suitable for applications that require transaction support and high concurrency, while MyISAM is suitable for applications that require more reads and less writes. 1.InnoDB supports transaction and bank-level locks, suitable for e-commerce and banking systems. 2.MyISAM provides fast read and indexing, suitable for blogging and content management systems.

What are the different types of JOINs in MySQL?What are the different types of JOINs in MySQL?Apr 27, 2025 am 12:13 AM

There are four main JOIN types in MySQL: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN returns all rows in the two tables that meet the JOIN conditions. 2.LEFTJOIN returns all rows in the left table, even if there are no matching rows in the right table. 3. RIGHTJOIN is contrary to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet JOIN conditions.

What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment