在一個(gè)數(shù)據(jù)庫(kù)中,可能存在多個(gè)表,這些表都是相互關(guān)聯(lián)的。我們繼續(xù)使用前面的例子。前面建立的表中包含了員工的一些基本信息,如姓名、性別、出生日期、出生地。我們?cè)賱?chuàng)建一個(gè)表,該表用于描述員工所發(fā)表的文章,內(nèi)容包括作者姓名、文章標(biāo)題、發(fā)表日期。
1、查看第一個(gè)表mytable的內(nèi)容:
mysql> select * from mytable;
+----------+------+------------+-----------+
| name | sex | birth | birthaddr |
+----------+------+------------+-----------+
| abccs|f | 1977-07-07 | china |
| mary |f | 1978-12-12 | usa |
| tom |m | 1970-09-02 | usa |
+----------+------+------------+-----------+
2、創(chuàng)建第二個(gè)表title(包括作者、文章標(biāo)題、發(fā)表日期):
mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);
向該表中填加記錄,最后表的內(nèi)容如下:
mysql> select * from title;
+--------+-------+------------+
| writer | title | senddate |
+--------+-------+------------+
| abccs | a1| 2000-01-23 |
| mary | b1| 1998-03-21 |
| abccs | a2| 2000-12-04 |
| tom| c1| 1992-05-16 |
| tom| c2| 1999-12-12 |
+--------+-------+------------+
5 rows in set (0.00sec)
3、多表查詢
現(xiàn)在我們有了兩個(gè)表: mytable 和 title。利用這兩個(gè)表我們可以進(jìn)行組合查詢:
例如我們要查詢作者abccs的姓名、性別、文章:
mysql> SELECT name,sex,title FROM mytable,title
-> WHERE name=writer AND name=’abccs’;
+-------+------+-------+
| name | sex | title |
+-------+------+-------+
| abccs | f| a1|
| abccs | f| a2|
+-------+------+-------+
上面例子中,由于作者姓名、性別、文章記錄在兩個(gè)不同表內(nèi),因此必須使用組合來(lái)進(jìn)行查詢。必須要指定一個(gè)表中的記錄如何與其它表中的記錄進(jìn)行匹配。
注意:如果第二個(gè)表title中的writer列也取名為name(與mytable表中的name列相同)而不是writer時(shí),就必須用mytable.name和title.name表示,以示區(qū)別。
再舉一個(gè)例子,用于查詢文章a2的作者、出生地和出生日期:
mysql> select title,writer,birthaddr,birth from mytable,title
-> where mytable.name=title.writer and title=’a2’;
+-------+--------+-----------+------------+
| title | writer | birthaddr | birth |
+-------+--------+-----------+------------+
| a2| abccs | china | 1977-07-07 |
+-------+--------+-----------+------------+
1、查看第一個(gè)表mytable的內(nèi)容:
mysql> select * from mytable;
+----------+------+------------+-----------+
| name | sex | birth | birthaddr |
+----------+------+------------+-----------+
| abccs|f | 1977-07-07 | china |
| mary |f | 1978-12-12 | usa |
| tom |m | 1970-09-02 | usa |
+----------+------+------------+-----------+
2、創(chuàng)建第二個(gè)表title(包括作者、文章標(biāo)題、發(fā)表日期):
mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);
向該表中填加記錄,最后表的內(nèi)容如下:
mysql> select * from title;
+--------+-------+------------+
| writer | title | senddate |
+--------+-------+------------+
| abccs | a1| 2000-01-23 |
| mary | b1| 1998-03-21 |
| abccs | a2| 2000-12-04 |
| tom| c1| 1992-05-16 |
| tom| c2| 1999-12-12 |
+--------+-------+------------+
5 rows in set (0.00sec)
3、多表查詢
現(xiàn)在我們有了兩個(gè)表: mytable 和 title。利用這兩個(gè)表我們可以進(jìn)行組合查詢:
例如我們要查詢作者abccs的姓名、性別、文章:
mysql> SELECT name,sex,title FROM mytable,title
-> WHERE name=writer AND name=’abccs’;
+-------+------+-------+
| name | sex | title |
+-------+------+-------+
| abccs | f| a1|
| abccs | f| a2|
+-------+------+-------+
上面例子中,由于作者姓名、性別、文章記錄在兩個(gè)不同表內(nèi),因此必須使用組合來(lái)進(jìn)行查詢。必須要指定一個(gè)表中的記錄如何與其它表中的記錄進(jìn)行匹配。
注意:如果第二個(gè)表title中的writer列也取名為name(與mytable表中的name列相同)而不是writer時(shí),就必須用mytable.name和title.name表示,以示區(qū)別。
再舉一個(gè)例子,用于查詢文章a2的作者、出生地和出生日期:
mysql> select title,writer,birthaddr,birth from mytable,title
-> where mytable.name=title.writer and title=’a2’;
+-------+--------+-----------+------------+
| title | writer | birthaddr | birth |
+-------+--------+-----------+------------+
| a2| abccs | china | 1977-07-07 |
+-------+--------+-----------+------------+