search
HomeDatabaseMysql TutorialWhat is the use of database query statement select?

The database query statement select is used to select data from the table, and the results are stored in a result table (called a result set); the syntax format is "SELECT [column name] FROM table name [WHERE clause and other restrictions];".

What is the use of database query statement select?

(Recommended tutorial: mysql video tutorial)

SELECT statement

The SELECT statement is used to select data from a table.

The results are stored in a results table (called a result set).

The syntax format of SELECT is as follows:

SELECT
{* | <字段列名>}
[
FROM <表 1>, <表 2>…
[WHERE <表达式>
[GROUP BY <group by definition>
[HAVING <expression> [{<operator> <expression>}…]]
[ORDER BY <order by definition>]
[LIMIT[<offset>,] <row count>]
]

Among them, the meaning of each clause is as follows:

  • ##{*|< ;Field column name>} A field list containing the asterisk wildcard character, indicating the name of the field to be queried.

  • ##,
    …, Table 1 and Table 2 represent the source of query data, which can be single or multiple.
  • WHERE is optional. If selected, the query data must meet the query conditions.
  • GROUP BY, this clause tells MySQL how to display the queried data and group it according to the specified field.
  • [ORDER BY], this clause tells MySQL in what order to display the queried data. The possible sorting is ascending order (ASC) and descending order (DESC ), which is ascending by default.
  • ##[LIMIT[,]], this clause tells MySQL to display the number of queried data items each time.
  • Use "*" to query all fields in the table

    SELECT You can use "*" to query data for all fields in the table. The syntax format is as follows :

    SELECT * FROM 表名;

    When using "*" to query, you can only sort the fields in the order in the data table, and you cannot change the order of the fields.

    Example 1

    Query the data of all fields from the tb_students_info table. The SQL statement and running results are as follows.

    mysql> use test_db;
    Database changed
    mysql> SELECT * FROM tb_students_info;
    +----+--------+---------+------+------+--------+------------+
    | id | name   | dept_id | age  | sex  | height | login_date |
    +----+--------+---------+------+------+--------+------------+
    |  1 | Dany   |       1 |   25 | F    |    160 | 2015-09-10 |
    |  2 | Green  |       3 |   23 | F    |    158 | 2016-10-22 |
    |  3 | Henry  |       2 |   23 | M    |    185 | 2015-05-31 |
    |  4 | Jane   |       1 |   22 | F    |    162 | 2016-12-20 |
    |  5 | Jim    |       1 |   24 | M    |    175 | 2016-01-15 |
    |  6 | John   |       2 |   21 | M    |    172 | 2015-11-11 |
    |  7 | Lily   |       6 |   22 | F    |    165 | 2016-02-26 |
    |  8 | Susan  |       4 |   23 | F    |    170 | 2015-10-01 |
    |  9 | Thomas |       3 |   22 | M    |    178 | 2016-06-07 |
    | 10 | Tom    |       4 |   23 | M    |    165 | 2016-08-05 |
    +----+--------+---------+------+------+--------+------------+
    10 rows in set (0.26 sec)

    The results show that when using the "*" wildcard character, all columns will be returned, and the data columns will be displayed in the order when the table was created.

    Note: Generally, unless you need to use all field data in the table, it is best not to use the wildcard "*". Although using wildcards can save time typing query statements, obtaining unnecessary column data often reduces the efficiency of the query and the application used. The advantage of using "*" is that when the names of the required columns are not known, they can be obtained via "*".

    Specified fields in the query table

    The syntax format of a certain field in the query table is:

    SELECT < 列名 > FROM < 表名 >;

    Example 2

    Query the names of all students in the name column in the tb_students_info table. The SQL statement and running results are as follows.

    mysql> SELECT name FROM tb_students_info;
    +--------+
    | name   |
    +--------+
    | Dany   |
    | Green  |
    | Henry  |
    | Jane   |
    | Jim    |
    | John   |
    | Lily   |
    | Susan  |
    | Thomas |
    | Tom    |
    +--------+
    10 rows in set (0.00 sec)

    The output shows all data under the name field in the tb_students_info table.

    Use the SELECT statement to obtain data under multiple fields. You only need to specify the field name to be searched after the keyword SELECT. Different field names are separated by commas "," after the last field. There is no need to add commas. The syntax format is as follows:

    SELECT <字段名1>,<字段名2>,…,<字段名n> FROM <表名>;

    Example 3

    Get the three columns of id, name and height from the tb_students_info table. The SQL statement and running results are as follows.

    mysql> SELECT id,name,height
        -> FROM tb_students_info;
    +----+--------+--------+
    | id | name   | height |
    +----+--------+--------+
    |  1 | Dany   |    160 |
    |  2 | Green  |    158 |
    |  3 | Henry  |    185 |
    |  4 | Jane   |    162 |
    |  5 | Jim    |    175 |
    |  6 | John   |    172 |
    |  7 | Lily   |    165 |
    |  8 | Susan  |    170 |
    |  9 | Thomas |    178 |
    | 10 | Tom    |    165 |
    +----+--------+--------+
    10 rows in set (0.00 sec)

    The output shows all data under the three fields of id, name and height in the tb_students_info table.

The above is the detailed content of What is the use of database query statement select?. For more information, please follow other related articles on the PHP Chinese website!

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
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

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 Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools