1. 程式人生 > 實用技巧 >DQL 資料查詢語言

DQL 資料查詢語言

查詢資料(SELECT)

# 查詢所有資料 — 很危險,資料量過大,容易導致記憶體溢位而宕機
mysql> select * from student;

# 先查詢資料總量,然後決定是否可以查詢所有資料
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.01 sec)

# 查詢指定列資料
mysql> select user,host from mysql.user;
+--------+------------+
| user   | host       |
+--------+------------+
| root   | %          |
| root   | 127.0.0.1  |
| lhd    | 172.16.1.% |
| zzzwqh | 172.16.1.% |
| root   | 172.16.1.% |
| root   | ::1        |
|        | db03       |
| root   | db03       |
|        | localhost  |
| root   | localhost  |
+--------+------------+
10 rows in set (0.01 sec)

條件查詢(SELECT,WHERE)

mysql> select name,gender from student where name='小王';
+--------+--------+
| name   | gender |
+--------+--------+
| 小王   | f      |
+--------+--------+
1 row in set (0.00 sec)

查詢練習

匯入一個 world 資料庫,點選下載,解壓即可

匯入資料

# 方式一:
[root@db03 ~]#  mysql -uroot -p123 < world.sql 

# 方式二:
mysql> source /root/world.sql;

# 方式三:
mysql> \. /root/world.sql;

查詢資料

mysql> use world;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)

mysql> select count(*) from city;
+----------+
| count(*) |
+----------+
|     4079 |
+----------+
1 row in set (0.00 sec)

mysql> select * from city;

# 1.查看錶結構
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

# 2.檢視所有資料
mysql> select * from city;

# 3.檢視指定列的資料
mysql> select Name,Population from city;

# 4.檢視資料時排序(按照人口數量)
# 升序
mysql> select Name,Population from city order by Population;
# 降序
mysql> select Name,Population from city order by Population desc;

# 5.查詢部分資料
# 檢視前十條資料
mysql> select Name,Population from city order by Population desc limit 10;

# 6.按照步長查詢資料,第一個 50 表示起始位置,第二個 50 表示步長
mysql> select id,Name,Population from city limit 50,50;
# 第一個 50 表示起始位置,第二個 50 表示步長

條件查詢

# 1.條件查詢就是使用where語句,where語句可以使用的符號
條件符號:= < > <= >= != <> or and like
	精確匹配:=
	範圍匹配:< > <= >= != <>
	模糊匹配:like
	連線語句:or and
	
# 2.查詢中國的城市人口
mysql> select name,population from city where CountryCode='CHN';

# 3.查詢黑龍江人口數量
mysql> select name,population from city where countrycode='CHN' and District='heilongjiang';

# 4.查詢中國人口數量小於 100000 的城市
mysql> select name,population from city where countrycode='CHN' and population < 100000;

# 5.模糊匹配
# 匹配以N結尾的資料
mysql> select name,countrycode from city where countrycode like '%N';
# 匹配以N開頭的資料
mysql> select name,countrycode from city where countrycode like 'N%';
# 匹配包含N的資料
mysql> select name,countrycode from city where countrycode like '%N%';

# 6.查詢中國或美國的人口數量
# 使用or
mysql> select name,population from city where countrycode = 'CHN' or countrycode = 'USA';
# 使用 in
mysql> select name,population from city where countrycode in ('CHN','USA');
# 使用 union all
mysql> select name,population from city where countrycode = 'CHN' union all select name,population from city where countrycode = 'USA';