时间:2021-07-01 10:21:17 帮助过:52人阅读
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 |
+-------+------+-------+