search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the use of SELECT

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 //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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools