Home Database Mysql Tutorial What is the use of database query statement select?

What is the use of database query statement select?

Oct 16, 2020 pm 02:44 PM
mysql 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>]
]
Copy after login

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< Field >, this clause tells MySQL how to display the queried data and group it according to the specified field.
  • [ORDER BY< field>], 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 表名;
    Copy after login

    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)
    Copy after login

    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 < 表名 >;
    Copy after login

    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)
    Copy after login

    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 <表名>;
    Copy after login

    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)
    Copy after login
    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 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 Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    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
    1248
    24
    MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

    The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

    Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

    Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

    MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

    MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

    MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

    I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

    Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

    Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

    MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

    When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

    See all articles