Home Backend Development PHP Tutorial Detailed explanation of the use of SELECT

Detailed explanation of the use of SELECT

Dec 15, 2016 pm 01:02 PM

The data table has been created. Assuming that we have inserted a lot of data, we can retrieve and display the information in the data table in the way we like. For example: we can display the entire data table as follows的内容都显示出来
        select * from president;
    也可以只选取某一个数据行里的某一个数据列
        select birth from president where last_name='Eisenhower';
    select语句的通用形式如下:
        select 你要Information 数据 From data table (one or more)
conditional conditions
SELECT statements have several clauses. Their various mixs can help you find the most interesting information. These sentences can be very simple or can be It’s very complicated, let’s see how the author explains it in detail

1, Use various operators to set the search conditions

If you want the select statement to retrieve only the records that meet specific conditions, you must add the where clause to it Set the search conditions for data rows. Only in this way can we selectively select those data rows whose data column values ​​meet specific requirements. You can search for any type of value, for example, search for numerical values
select * from score where score>95; //Display all information with scores above 95 points
You can also search for string values
select last_name, first_name from president where last_name='Tom'; //Find all presidents with the last name tom
You can also perform a combined search of different types of values
select last_name, first_name,birth,state from president
where birth<'1950-1- 1' and (state='VA' or state='BA');
//Find the president who was born in VA or BA before 1950
It can be seen that the arithmetic operator (+- */%), comparison operators (<>=) and logical operators. We should be proficient in understanding the meaning of these operators (all simple)

2, special handling of NULL values ​​

This is a kind of Any type of value. It is usually used to mean "no data", "unknown data", "missing data", "data outside the value range", "irrelevant to this data column", "different from other values ​​in this data column" and other meanings. NULL values ​​are useful in many situations.
Our various operators cannot process NULL values. If you search for NULL values, use is null or is not null to judge. For example:
select last_name, first_name, birth, state from president
where death is null; //Find all the presidents who are not dead
In some cases, the NULL value is a very useful type, and everyone will gradually understand it.

3, Sorting query results

Generally speaking, if you create a data table and insert some records into it, when a select * from name command is issued, the order of the data records in the query results is usually the same as their order The order in which they are inserted is the same. This is certainly in line with our thinking habits. But this is just a "taken for granted" assumption. In fact, when records are deleted, some empty areas will be generated in the database, and MYSQL will fill these areas with new records. In other words, this assumption is Incorrect. Therefore, we must remember that there is no guarantee in the order of the record lines returned from the server! If you want to follow a certain order, you must use the order by clause to set the order.
select last_name,first_name,birth,state from president
order by last_name; //Let the names of the presidents be arranged in alphabetical order
You can also set the ascending and descending order of the arrangement
select last_name,first_name from president
order by state DESC,last_name ASC ;
//First sort in descending order of birthplace, and those with the same birthplace in ascending order of last name
Note: If the results contain NULL values, by default they always appear at the beginning of the query results.
4 Just want to see the first 5

order by birth limit 10,5; //Return the 5 records starting from the 11th record (skipping 10)

Tip: Randomly find a president from the president table to play with :
select last_name, first_name, birth, state from president
order by rand() limit 1; //This is an expression evaluation method, it works everywhere
列5, to value the output column and naming

In order to improve the efficiency, mysql can also use the calculation result of the expression as the value of the output column. Expressions can be simple or complex. For example: The following query has two output columns, the former output

The above is the detailed explanation of the use of SELECT. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles