SQL語句之DQL數據查詢語言(select、desc)


1.select查詢語句

1.查詢表中所有的數據

# 很危險,數據量過大,容易導致down機
mysql> select * from student;

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

2.查看指定列的數據

mysql> select user,host from mysql.user;
+--------+------------+
| user   | host       |
+--------+------------+
| root   | %          |
| root   | 127.0.0.1  |
| lhd    | 172.16.1.% |
| qiudao | 172.16.1.% |
| root   | 172.16.1.% |
| root   | ::1        |
|        | db03       |
| root   | db03       |
|        | localhost  |
| root   | localhost  |
+--------+------------+
10 rows in set (0.01 sec)

3.按條件查詢

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

2.簡單查詢練習

1.將sql文件導入數據

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

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

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

2.查看數據

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;

3.查詢練習

# 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.按照步長查詢數據
mysql> select id,Name,Population from city limit 50,50;
										#50起始位置  50步長

3.條件查詢

# 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';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM