使用select查询数据

在SQL中,使用select语句来查询数据。不同的关系数据库,select语法会有细微差别,在MySQL 官网 ​​https://dev.mysql.com/doc/refman/8.0/en/select.html​​ 可以查到支持的select语法。

SELECT column_name1, column_name2
FROM table_name
[WHERE where_condition]
[GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]

实战案例

mysql> select * from person;
+----+------+------------------+
| id | name | id_number |
+----+------+------------------+
| 1 | Lily | 1234567890123478 |
| 3 | Gray | 1234567890123479 |
+----+------+------------------+
2 rows in set (0.00 sec)

mysql> select name, id_number from person;
+------+------------------+
| name | id_number |
+------+------------------+
| Lily | 1234567890123478 |
| Gray | 1234567890123479 |
+------+------------------+
2 rows in set (0.00 sec)

mysql> select name, id_number from person where name='Lily';
+------+------------------+
| name | id_number |
+------+------------------+
| Lily | 1234567890123478 |
+------+------------------+
1 row in set (0.00 sec)

mysql> select 78 * 34;
+---------+
| 78 * 34 |
+---------+
| 2652 |
+---------+
1 row in set (0.00 sec)

mysql>