下面讓我們真正開始學(xué)習(xí)數(shù)據(jù)庫吧。
Part4.檢索數(shù)據(jù)-select
- 4.1-簡單創(chuàng)建一個數(shù)據(jù)表與插入數(shù)據(jù)--了解mysql
- 4.2-檢索單個列
- 4.3-檢索多個列
- 4.4-檢索所有列
- 4.5-檢索不同的行-關(guān)鍵字-distinct
- 4.6 -限制返回結(jié)果數(shù)量-關(guān)鍵字-limit
- 4.7 -使用完全限定的表名
Part4.1 簡單創(chuàng)建一個表
查看所有數(shù)據(jù)庫: show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| first |
| mysql |
| performance_schema |
| sys |
+--------------------+
我們看到了上次建的數(shù)據(jù)庫first回俐,現(xiàn)在我們使用它
use first;
- 創(chuàng)建一個數(shù)據(jù)表:
mysql> create table my_class(
-> id int(11) unsigned not null primary key auto_increment,
-> age tinyint,
-> name varchar(100));
Query OK, 0 rows affected (0.06 sec)
一定要自己動手打保礼,不然沒什么用
id,age,name 都是字段(列),
int(11) : tinyint,vachar(100)都是 數(shù)據(jù)類型
現(xiàn)在是要簡單了解這個就行了,后面有專門的章節(jié).
想看的話-->mysql數(shù)據(jù)類型信粮。
- 插入數(shù)據(jù)
使用insert into 表名(字段1,字段2...) values(值1,值2....);
mysql> insert into my_class(age,name) values(10,'hello');
Query OK, 1 row affected (0.00 sec)
mysql> insert into my_class(age,name) values(20,'world');
Query OK, 1 row affected (0.00 sec)
mysql> insert into my_class(age,name) values(30,'mysql');
Query OK, 1 row affected (0.00 sec)
mysql> insert into my_class(age,name) values(30,'php');
Query OK, 1 row affected (0.00 sec)
mysql> insert into my_class(age,name) values(40,'mysql');
Query OK, 1 row affected (0.00 sec)
select, insert ,into ,values,use ,show ,int....這些都是關(guān)鍵字
4.2-檢索單個列
假設(shè)這樣一個場景:
班級里所有人的姓名
語句:
mysql> select name from my_class;
+-------+
| name |
+-------+
| hello |
| world |
| mysql |
| php |
| mysql |
+-------+
- 結(jié)束語句捻悯,在mysql 中SQL語句以 分號(;)結(jié)束
- SQL語句不區(qū)分大小寫,但是一般在mysql都會將關(guān)鍵字大寫北戏,數(shù)據(jù)庫名,表名漫蛔,字段小寫嗜愈,比如,上面的檢索數(shù)據(jù) SELECT name FROM my_class;
4.3 -檢索多個列
班級里所有人的年齡和姓名
mysql> select age,name from my_class;
+------+-------+
| age | name |
+------+-------+
| 10 | hello |
| 20 | world |
| 30 | mysql |
| 30 | php |
| 40 | mysql |
+------+-------+
4.4 -檢索所有列
班級里所有人的 學(xué)號 莽龟,年齡蠕嫁,姓名
使用通配符(*)來完成任務(wù)
mysql> select * from my_CLASS;
+----+------+-------+
| id | age | name |
+----+------+-------+
| 1 | 10 | hello |
| 2 | 20 | world |
| 3 | 30 | mysql |
| 4 | 30 | php |
| 5 | 40 | mysql |
+----+------+-------+
發(fā)現(xiàn)了嗎 我用my_CLASS 也是可以獲取數(shù)據(jù)的,mysql是不區(qū)分大小寫的毯盈。
4.5-檢索不同的行-關(guān)鍵字-distinct
distinct:不同的
假設(shè)這樣一個場景
班級發(fā)獎剃毒,每個年齡的人發(fā)一次
mysql> select * from my_class;
+----+------+-------+
| id | age | name |
+----+------+-------+
| 1 | 10 | hello |
| 2 | 20 | world |
| 3 | 30 | mysql |
| 4 | 30 | php |
| 5 | 40 | mysql |
+----+------+-------+
mysql> select distinct age from my_class;
+------+
| age |
+------+
| 10 |
| 20 |
| 30 |
| 40 |
+------+
去掉不同年齡成功
mysql> select distinct age,name from my_class;
+------+-------+
| age | name |
+------+-------+
| 10 | hello |
| 20 | world |
| 30 | mysql |
| 30 | php |
| 40 | mysql |
+------+-------+
發(fā)現(xiàn)這次并沒有把30-mysql,40-mysql 排除,那是因為distinct關(guān)鍵字作用于所有列搂赋,必須age赘阀,name都相同才被排除掉
- 4.6 -限制返回結(jié)果數(shù)量-關(guān)鍵字-limit
返回3行數(shù)據(jù)
mysql> select age, name from my_class limit 3;
+------+-------+
| age | name |
+------+-------+
| 10 | hello |
| 20 | world |
| 30 | mysql |
+------+-------+
limit 1000,就返回1000行數(shù)據(jù)么?
肯定不是脑奠。基公。limit 1000是返回不超過1000行的數(shù)據(jù),數(shù)量<=1000
mysql> select age,name from my_class limit 1000;
+------+-------+
| age | name |
+------+-------+
| 10 | hello |
| 20 | world |
| 30 | mysql |
| 30 | php |
| 40 | mysql |
+------+-------+
返回第一條數(shù)據(jù)
mysql> select age,name from my_class limit 0,1;
+------+-------+
| age | name |
+------+-------+
| 10 | hello |
+------+-------+
檢索出來的數(shù)據(jù)是從0開始的而不是1宋欺,因此limit 2,1 就是從第三條數(shù)據(jù)開始轰豆,獲取最多1條數(shù)據(jù)
mysql> select age,name from my_class limit 2,1;
+------+-------+
| age | name |
+------+-------+
| 30 | mysql |
+------+-------+
- 4.7 -使用完全限定的表名
就是數(shù)據(jù)庫名.表名.字段名 或者數(shù)據(jù)庫名.表名
mysql> select first.my_class.name from first.my_class;
+-------+
| name |
+-------+
| hello |
| world |
| mysql |
| php |
| mysql |
+-------+
to be continue