MySQL 多表查詢實現分析

來源:互聯網
上載者:User

標籤:data   mysqldump   color   例子   arc   code   指定   增加   col   

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);       向該表中填加記錄,最後表的內容如下:   <ccid_nobr>   <table width="400" border="1" cellspacing="0" cellpadding="2"   bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">   <tr>   <td bgcolor="e6e6e6" "font-size:9pt">   <pre><ccid_code> 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。利用這兩個表我們可以進行組合查詢:   上面例子中,由於作者姓名、性別、文章記錄在兩個不同表內,因此必須使用組合來進行查詢。必須要指定一個表中的記錄如何與其它表中的記錄進行匹配。   注意:如果第二個表 title 中的 writer 列也取名為 name(與mytable表中的name列相同)而不是 write r時,就必須用 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 |   +----------+------+------------+-----------+--------+   3、增加記錄   前面已經講過如何增加一條記錄,為便於查看,重複與此:   mysql> insert into mytable   -> values (′abc′,′f′,′1966-08-17′,′china′,′n′);   Query OK, 1 row affected (0.05 sec)   查看一下:   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 |   | abc |f | 1966-08-17 | china | n |   +----------+------+------------+-----------+--------+   4、刪除記錄   用如下命令刪除表中的一條記錄:mysql> delete from mytable where name=′abc′;   DELETE 從表中刪除滿足由 where 給出的條件的一條記錄。再顯示一下結果:   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 |   +----------+------+------------+-----------+--------+   5、刪除表:   mysql> drop table ****(表 1 的名字),*** 表 2 的名字; 可以刪除一個或多個表,小心使用。   6、資料庫的刪除:   mysql> drop database 資料庫名; 小心使用。   7、資料庫的備份:   退回到 DOS:   mysql> quit   d:\mysqlbin   使用如下命令對資料庫 abccs 進行備份:   mysqldump --opt abccs>abccs.dbb   abccs.dbb 就是你的資料庫 abccs 的備份檔案。   8、用批處理方式使用 MySQL:   首先建立一個批次檔 mytest.sql,內容如下:   use abccs;   select * from mytable;   select name,sex from mytable where name=′abccs′; 

  

MySQL 多表查詢實現分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.