Home >Database >Mysql Tutorial >How to query multiple fields in mysql

How to query multiple fields in mysql

青灯夜游
青灯夜游Original
2022-01-04 13:53:5512669browse

In mysql, you can use the SELECT statement to query multiple field data, the syntax is "SELECT field name 1, field name 2,..., field name n FROM data table name [WHERE clause];" ; Multiple field names need to be separated by English commas ",".

How to query multiple fields in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In MySQL, you can use the SELECT statement to query data. Querying data refers to using different query methods to obtain different data from the database according to needs. It is the most frequently used and important operation.

I want to query a specified field in the table

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

SELECT 列名 FROM 表名 [WHERE子句];

Example

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.

Want to query multiple fields specified in the table

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

SELECT 字段名1,字段名2,…,字段名n FROM 表名 [WHERE子句];

Example

Get it from the tb_students_info table The three columns of id, name and height, 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.

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to query multiple fields in mysql. 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