Home Database Mysql Tutorial Mysqlsql语句回顾1_MySQL

Mysqlsql语句回顾1_MySQL

Jun 01, 2016 pm 01:04 PM
statement

检索出不存在相同值的列表可以加上DISTINCT关键字

SELECT DISTINCT vend_id FROM products;

可以使用LIMIT子句来限制返回结果的数量

SELECT prod_name FROM products LIMIT 5;

LIMIT子句同样拥有offset的功能,就是设定从第几条数据开始获得

SELECT prod_name FROM products LIMIT 5,5;

代表从第五个数据开始的五条数据

虽然LIMIT具有offset的功能,但是Mysql还是提供了OFFSET子句

SELECT prod_name FROM products LIMIT 5 OFFSET 5;

mysql的索引值是从0开始的

可以使用ORDER BY子句来给数据进行排序,该子句取一个或多个列的名字,据此对输出进行排序

SELECT prod_name FORM products ORDER BY prod_name;

该子句可是用非检索的列排序数据

ORDER BY子句可以选择使用多个列排序,所选列之间使用逗号隔开。

ORDER BY 子句要仿造WHERE子句之后

ORDER BY子句可以指定排序的方向(升序和降序),默认是升序,如果想变为降序需要在ORDER BY 子句的最后加上DESC

DESC关键字只应用到直接位于其前面的列名,可以使用如下的查询方式

SELECT prod_name,prod_price,prod_id FROM products ORDER BY prod_name DESC,prod_name;

WHERE 子句的操作符号中判断相等使用=判断不相等使用符号,和编程语言当中略显不同

通过使用BETWEEN关键字来进行范围值检查

SELECT prod_name,prod_price FROM products WHERE prod_price BETWEEN 5 AND 10;

使用IS NULL来进行空值检查

SELECT prod_name FROM products WHERE prod_price IS NULL

该语句返回没有价格的产品数据

mysql中存在几个逻辑操作符,分别是以下几个

AND OR IN NOT

SELECT prod_id,prod_price,prod_name FROM products WHERE vend_id=1003 AND prod_price

SELECT prod_name,prod_price FROM products WHERE vend_id=1002 OR vend_id=1003;取并集

SELECT prod_name,prod_price FROM products WHERE vend_id IN(1002,1003) ORDER BY prod_name; 取交集

SELECT prod_name,prod_price FROM products WHERE vend_id NOT IN(1002,1003) ORDER BY prod_name; 取并集的补集

可以使用圆括号来标记计算次序

%通配符表示任何字符出现任意次序,使用LIKE操作符

SELECT prod_id,prod_name FROM products WHERE prod_name LIKE ‘jet%’;

该查询语句会查询所有prod_name字段以jet开头的行

_通配符匹配单个任意字符

SELECT prod_id,prod_name FROM products WHERE prod_name LIKE ‘_ ton anvil’;

该查询语句查询任何以ton anvil 为结尾并且在其之前只有一个字符的所有行

不要过度使用通配符,如果其他操作能达到相同的目的,应该使用其他操作符。

在确实需要通配符时,除非绝对有必要,否则不要把它们用在搜索模式的开始处,这样最慢。

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

To understand flow control statements in Python, you need to master several situations To understand flow control statements in Python, you need to master several situations Jan 20, 2024 am 08:06 AM

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

C# try statement usage C# try statement usage Feb 22, 2024 pm 12:45 PM

The usage of try statement in C# requires specific code examples. C# is an object-oriented programming language, in which the try statement is a structure used to capture and handle exceptions. Through the try statement, we can write code to handle exceptions that may occur, thereby improving the stability and reliability of the program. In this article, we will introduce the usage of try statement in C# and provide some specific code examples to help readers understand. In C#, the try statement consists of try block, catch block and optionalfina

Master the skills of Go statements in C language Master the skills of Go statements in C language Mar 23, 2024 pm 12:39 PM

Using Go statements in C language is a common and flexible programming technique that can achieve concurrent execution. The following will introduce some techniques for mastering Go statements in C language, with specific code examples. First, we need to understand how to simulate concurrent execution in C language. In C language, concurrent operations can be achieved by creating multiple threads. In multi-threaded programming, we can use the pthread library to create and manage threads. Next is a simple C language program in which two threads are created and executed concurrently

Master the types of Python flow control statements and learn from scratch! Master the types of Python flow control statements and learn from scratch! Jan 20, 2024 am 09:02 AM

To learn Python from scratch, first understand the types of flow control statements! Python is a simple and powerful programming language that is widely used in data analysis, artificial intelligence, network development and various scientific computing fields. As a beginner, it is very important to master basic flow control statements, because they are the basis for realizing logical judgment and controlling the program execution flow. In Python, there are three main types of flow control statements: sequential structures, conditional structures and loop structures. The following will introduce these three process control statements in detail and give the corresponding

The purpose of SQL ALTER statement The purpose of SQL ALTER statement Feb 19, 2024 pm 05:01 PM

The function of the SQL ALTER statement requires specific code examples. In a database management system, the ALTER statement is a SQL command used to modify database objects. Through the ALTER statement, we can modify database objects such as tables, columns, indexes, and views, including adding, deleting, modifying, and other operations. The following will introduce the common usage of the ALTER statement in detail and provide relevant code examples. ALTERTABLE statement is used to modify the structure of the table. You can add, delete, modify columns, constraints, indexes, etc.

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

See all articles