/

SQL,如何使用SELECT

SQL,如何使用SELECT

如何從SQL數據庫表中獲取數據

您可以使用SELECT命令從表中獲取數據。

獲取所有行和列:

1
SELECT * FROM people;
1
2
3
4
age | name
-----+--------
37 | Flavio
8 | Roger

只獲取name列:

1
SELECT name FROM people;
1
2
3
4
name
--------
Flavio
Roger

計算表中的項目數量:

1
SELECT COUNT(*) from people;
1
2
3
count
-------
2

您可以使用WHERE子句過濾表中的行:

1
SELECT age FROM people WHERE name='Flavio';
1
2
3
age
-----
37

查詢的結果可以按列值升序(默認)或降序使用ORDER BY進行排序:

1
SELECT * FROM people ORDER BY name;
1
SELECT * FROM people ORDER BY name DESC;

tags: [“SQL”, “SELECT”, “WHERE”, “ORDER BY”]