MySQL 多表查詢實現(xiàn)分析
更新時間:2011年06月28日 22:14:00 作者:
在一個數(shù)據(jù)庫中,可能存在多個表,這些表都是相互關(guān)聯(lián)的。
我們繼續(xù)使用前面的例子。前面建立的表中包含了員工的一些基本信息,如姓名、性別、出生日期、出生地。我們再創(chuàng)建一個表,該表用于描述員工所發(fā)表的文章,內(nèi)容包括作者姓名、文章標(biāo)題、發(fā)表日期。
1、查看第一個表 mytable 的內(nèi)容:
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、創(chuàng)建第二個表 title (包括作者、文章標(biāo)題、發(fā)表日期):
mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);
向該表中填加記錄,最后表的內(nèi)容如下:
<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、多表查詢
現(xiàn)在我們有了兩個表: mytable 和 title。利用這兩個表我們可以進行組合查詢:
上面例子中,由于作者姓名、性別、文章記錄在兩個不同表內(nèi),因此必須使用組合來進行查詢。必須要指定一個表中的記錄如何與其它表中的記錄進行匹配。
注意:如果第二個表 title 中的 writer 列也取名為 name(與mytable表中的name列相同)而不是 write r時,就必須用 mytable.name 和 title.name 表示,以示區(qū)別。
再舉一個例子,用于查詢文章 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 |
+-------+--------+-----------+------------+
修改和備份、批處理
有時我們要對數(shù)據(jù)庫表和數(shù)據(jù)庫進行修改和刪除,可以用如下方法實現(xiàn):
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′; 現(xiàn)在來看看發(fā)生了什么:
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、增加記錄
前面已經(jīng)講過如何增加一條記錄,為便于查看,重復(fù)與此:
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 給出的條件的一條記錄。再顯示一下結(jié)果:
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、數(shù)據(jù)庫的刪除:
mysql> drop database 數(shù)據(jù)庫名; 小心使用。
7、數(shù)據(jù)庫的備份:
退回到 DOS:
mysql> quit
d:\mysqlbin
使用如下命令對數(shù)據(jù)庫 abccs 進行備份:
mysqldump --opt abccs>abccs.dbb
abccs.dbb 就是你的數(shù)據(jù)庫 abccs 的備份文件。
8、用批處理方式使用 MySQL:
首先建立一個批處理文件 mytest.sql,內(nèi)容如下:
use abccs;
select * from mytable;
select name,sex from mytable where name=′abccs′;
在 DOS 下運行如下命令:d:mysqlbin mysql < mytest.sql
在屏幕上會顯示執(zhí)行結(jié)果。
如果想看結(jié)果,而輸出結(jié)果很多,則可以用這樣的命令: mysql < mytest.sql | more
我們還可以將結(jié)果輸出到一個文件中: mysql < mytest.sql > mytest.out
1、查看第一個表 mytable 的內(nèi)容:
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、創(chuàng)建第二個表 title (包括作者、文章標(biāo)題、發(fā)表日期):
mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);
向該表中填加記錄,最后表的內(nèi)容如下:
<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、多表查詢
現(xiàn)在我們有了兩個表: mytable 和 title。利用這兩個表我們可以進行組合查詢:
上面例子中,由于作者姓名、性別、文章記錄在兩個不同表內(nèi),因此必須使用組合來進行查詢。必須要指定一個表中的記錄如何與其它表中的記錄進行匹配。
注意:如果第二個表 title 中的 writer 列也取名為 name(與mytable表中的name列相同)而不是 write r時,就必須用 mytable.name 和 title.name 表示,以示區(qū)別。
再舉一個例子,用于查詢文章 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 |
+-------+--------+-----------+------------+
修改和備份、批處理
有時我們要對數(shù)據(jù)庫表和數(shù)據(jù)庫進行修改和刪除,可以用如下方法實現(xiàn):
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′; 現(xiàn)在來看看發(fā)生了什么:
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、增加記錄
前面已經(jīng)講過如何增加一條記錄,為便于查看,重復(fù)與此:
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 給出的條件的一條記錄。再顯示一下結(jié)果:
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、數(shù)據(jù)庫的刪除:
mysql> drop database 數(shù)據(jù)庫名; 小心使用。
7、數(shù)據(jù)庫的備份:
退回到 DOS:
mysql> quit
d:\mysqlbin
使用如下命令對數(shù)據(jù)庫 abccs 進行備份:
mysqldump --opt abccs>abccs.dbb
abccs.dbb 就是你的數(shù)據(jù)庫 abccs 的備份文件。
8、用批處理方式使用 MySQL:
首先建立一個批處理文件 mytest.sql,內(nèi)容如下:
use abccs;
select * from mytable;
select name,sex from mytable where name=′abccs′;
在 DOS 下運行如下命令:d:mysqlbin mysql < mytest.sql
在屏幕上會顯示執(zhí)行結(jié)果。
如果想看結(jié)果,而輸出結(jié)果很多,則可以用這樣的命令: mysql < mytest.sql | more
我們還可以將結(jié)果輸出到一個文件中: mysql < mytest.sql > mytest.out
相關(guān)文章
解鎖?SQL?Server?2022的時間序列數(shù)據(jù)功能(示例詳解)
SQL?Server2022在處理時間序列數(shù)據(jù)時,SQL?Server?提供了一些優(yōu)化和功能,比如?DATE_BUCKET?函數(shù)、窗口函數(shù)以及其他時間日期函數(shù),以便更高效地處理時間序列數(shù)據(jù),這篇文章主要介紹了解鎖?SQL?Server?2022的時間序列數(shù)據(jù)功能,需要的朋友可以參考下2024-08-08SQL語句練習(xí)實例之一——找出最近的兩次晉升日期與工資額
程序員們在編寫一個雇員報表,他們需要得到每個雇員當(dāng)前及歷史工資狀態(tài)的信息,以便生成報表。報表需要顯示每個人的晉升日期和工資數(shù)目。2011-10-10數(shù)據(jù)庫Left join , Right Join, Inner Join 的相關(guān)內(nèi)容,非常實用
Left join , Right Join, Inner Join 的相關(guān)內(nèi)容,非常實用2009-07-07SQL 雙親節(jié)點查找所有子節(jié)點的實現(xiàn)方法
下面小編就為大家?guī)硪黄猄QL 雙親節(jié)點查找所有子節(jié)點的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05SQL高級應(yīng)用之使用SQL查詢Excel表格數(shù)據(jù)的方法
本文和大家講下如何在SQL Server分析器中查詢Excel電子表格的數(shù)據(jù),其實很簡單的,來看下下面的SQL語句吧。2010-03-03SQLServer 數(shù)據(jù)庫開發(fā)頂級技巧
無論你的專業(yè)水平如何,從其他IT專家那里學(xué)習(xí)新的技巧與最佳實踐常常都是有益的。本文包含了我遇到過的SQL Server開發(fā)的高級技巧。希望其中的一些技巧能夠?qū)δ臄?shù)據(jù)庫開發(fā)及管理工作有所幫助。2009-07-07