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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:59:03
Original
719 people have browsed it

   上面例子中,由于作者姓名、性别、文章记录在两个不同表内,因此必须使用组合来进行查询。必须要指定一个表中的记录如何与其它表中的记录进行匹配。

  注意:如果第二个表title中的writer列也取名为name(与mytable表中的name列相同)而不是writer时,就必须用mytable.name和title.name表示,以示区别。

  再举一个例子,用于查询文章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、增加一列:

  如在前面例子中的mytable表中增加一列表示是否单身single:


  mysql> alter table mytable add column single char(1);


  2、修改记录

  将abccs的single记录修改为“y”:


  mysql> update mytable set single=′y′ where name=′abccs′;


  现在来看看发生了什么:

 

mysql> select * from mytable;
+----------+------+------------+-----------+--------+
|   name   |  sex |    birth   | birthaddr | single |
+----------+------+------------+-----------+--------+
|   abccs  |   f  | 1977-07-07 |   china   |    y   |
|    mary  |   f  | 1978-12-12 |    usa    |  NULL  |
|    tom   |   m  | 1970-09-02 |    usa    |  NULL  |
+----------+------+------------+-----------+--------+
 

 

Related labels:
Statement of this Website
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
Latest Issues
Update one column of data in the table
From 1970-01-01 08:00:00
0
0
0
Optimize table indexes in MySQL
From 1970-01-01 08:00:00
0
0
0
Why can't the list be rendered?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template