1、查看第一個表mytable的內容:
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、建立第二個表title(包括作者、文章標題、發表日期):
mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);
向該表中填加記錄,最後表的內容如下:
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、多表查詢
現在我們有了兩個表: mytable 和 title。利用這兩個表我們可以進行組合查詢:
例如我們要查詢作者abccs的姓名、性別、文章:
mysql> SELECT name,sex,title FROM mytable,title
-> WHERE name=writer AND name='abccs';
+-------+------+-------+
| name | sex | title |
+-------+------+-------+
| abccs | f | a1 |
| abccs | f | a2 |
+-------+------+-------+