Home >Database >Mysql Tutorial >MySQL中多表操作和批处理方法(1)_MySQL

MySQL中多表操作和批处理方法(1)_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:59:03853browse

  在一个数据库中,可能存在多个表,这些表都是相互关联的。我们继续使用前面的例子。前面建立的表中包含了员工的一些基本信息,如姓名、性别、出生日期、出生地。我们再创建一个表,该表用于描述员工所发表的文章,内容包括作者姓名、文章标题、发表日期。

  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  |
+-------+------+-------+
 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn