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

Detailed explanation of the use of SELECT

黄舟
Release: 2023-03-03 14:12:02
Original
2259 people have browsed it

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 (m.sbmmt.com)!


Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template