mysql> select * from aritst; ERROR 1146 (42S02): Table 'temp.aritst' doesn't exist mysql> select * from artist; +------+----------+ | a_id | name | +------+----------+ | 1 | Da Vince | | 2 | Monet | | 4 | Pcasso | | 5 | Renoir | | 3 | Van Gogh | +------+----------+ 5 rows in set (0.00 sec) mysql> insert into painting values(1,null,'The Last Supper','IN',34),(1,null,'Teh Mo -> (3,NULL,'The Potato Eaters','KY',67),(3,NULL,'The Rocks','IA',33),(5,null,'L Query OK, 6 rows affected (0.08 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM PAINTING; +------+------+--------------------+-------+-------+ | a_id | p_id | title | state | price | +------+------+--------------------+-------+-------+ | 1 | 1 | The Last Supper | IN | 34 | | 1 | 2 | Teh Mona Lisa | MI | 87 | | 3 | 3 | Starry Night | KY | 48 | | 3 | 4 | The Potato Eaters | KY | 67 | | 3 | 5 | The Rocks | IA | 33 | | 5 | 6 | Last Deux Soeurs | NE | 64 |
| 6 | 7 | Three Country | CN | 64 |
+------+------+--------------------+-------+-------+6 rows in set (0.00 sec) |
+------+----------+------+--------------------+-------+-------+
| a_id | name | p_id | title | state | price |
+------+----------+------+--------------------+-------+-------+
| 1 | Da Vince | 1 | The Last Supper | IN | 34 |
| 1 | Da Vince | 2 | Teh Mona Lisa | MI | 87 |
| 5 | Renoir | 6 | Last Deux Soeurs | NE | 64 |
| 3 | Van Gogh | 3 | Starry Night | KY | 48 |
| 3 | Van Gogh | 4 | The Potato Eaters | KY | 67 |
| 3 | Van Gogh | 5 | The Rocks | IA | 33 |
+------+----------+------+--------------------+-------+-------+
连接和索引
因为连接会很容易导致MYSQL产生大量的行组合,所以确认你要比较的列已经被索引是个好的方式。否则,在表现模增长时性能会下降的很快。对于artist和painting表连接是基于每个表中的a_id列值产生的,如果int向前查看这些表的建表语句,你会发现在每个表中a_id都建立了索引*************************** 1. row *************************** Table: artist Create Table: CREATE TABLE `artist` ( `a_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, PRIMARY KEY (`a_id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.06 sec) ERROR: No query specified mysql> SHOW CREATE TABLE PAINTING\G *************************** 1. row *************************** Table: PAINTING Create Table: CREATE TABLE `painting` ( `a_id` int(10) unsigned NOT NULL, `p_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(30) NOT NULL, `state` varchar(30) NOT NULL, `price` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`p_id`), KEY `a_id` (`a_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) |
+------+----------+------+------+--------------------+-------+-------+
| a_id | name | a_id | p_id | title | state | price |
+------+----------+------+------+--------------------+-------+-------+
| 1 | Da Vince | 1 | 1 | The Last Supper | IN | 34 |
| 1 | Da Vince | 1 | 2 | Teh Mona Lisa | MI | 87 |
| 2 | Monet | NULL | NULL | NULL | NULL | NULL |
| 4 | Pcasso | NULL | NULL | NULL | NULL | NULL |
| 5 | Renoir | 5 | 6 | Last Deux Soeurs | NE | 64 |
| 3 | Van Gogh | 3 | 3 | Starry Night | KY | 48 |
| 3 | Van Gogh | 3 | 4 | The Potato Eaters | KY | 67 |
| 3 | Van Gogh | 3 | 5 | The Rocks | IA | 33 |
+------+----------+------+------+--------------------+-------+-------+
8 rows in set (0.00 sec)
mysql> select * from artist right join painting on artist.a_id = painting.a_id;+------+----------+------+------+--------------------+-------+-------+| a_id | name | a_id | p_id | title | state | price |+------+----------+------+------+--------------------+-------+-------+| 1 | Da Vince | 1 | 1 | The Last Supper | IN | 34 || 1 | Da Vince | 1 | 2 | Teh Mona Lisa | MI | 87 || 3 | Van Gogh | 3 | 3 | Starry Night | KY | 48 || 3 | Van Gogh | 3 | 4 | The Potato Eaters | KY | 67 || 3 | Van Gogh | 3 | 5 | The Rocks | IA | 33 || 5 | Renoir | 5 | 6 | Last Deux Soeurs | NE | 64 || NULL | NULL | 6 | 7 | Three Coutry | CN | 100 |+------+----------+------+------+--------------------+-------+-------+
编写LEFT JOIN 或者 RIGHT JOIN 查询的其他方法
和内链接一样有这样简便的写法,假如在两个表中存在相同名称的列,而且你又是通过这相同的列进行连接,那么就可以写成下面的形式:select * from tab1 left join tab2 on tab1.col = tab2.col;select * from tab1 left join tab2 using(col); select * from tab1 right join tab2 on tab1.col = tab2.col;select * from tab1 right join tab2 using(col); 在特殊的情况下,你希望根据两个表中的所有列进行比较,那么可以使用NATURALLEFT JOIN 或者 NATURAL RIGHT JOIN 并且省略ON或 USING mysql> select * from artist NATURAL left join painting -> ; +------+----------+------+--------------------+-------+-------+ | a_id | name | p_id | title | state | price | +------+----------+------+--------------------+-------+-------+ | 1 | Da Vince | 1 | The Last Supper | IN | 34 | | 1 | Da Vince | 2 | Teh Mona Lisa | MI | 87 | | 2 | Monet | NULL | NULL | NULL | NULL | | 4 | Pcasso | NULL | NULL | NULL | NULL | | 5 | Renoir | 6 | Last Deux Soeurs | NE | 64 | | 3 | Van Gogh | 3 | Starry Night | KY | 48 | | 3 | Van Gogh | 4 | The Potato Eaters | KY | 67 | | 3 | Van Gogh | 5 | The Rocks | IA | 33 | +------+----------+------+--------------------+-------+-------+ |
mysql> select * from painting p1 inner join painting p2 on p1.a_id = p2.a_idand p1.title='The Rocks'and p1.title !=p2.title;//要求:取出作品为The Rocks 并且删除自身的那条记录+------+------+-----------+-------+-------+------+------+--------------------+-------+-------+
| a_id | p_id | title | state | price | a_id | p_id | title | state | price |
+------+------+-----------+-------+-------+------+------+--------------------+-------+-------+
| 3 | 5 | The Rocks | IA | 33 | 3 | 3 | Starry Night | KY | 48 |
| 3 | 5 | The Rocks | IA | 33 | 3 | 4 | The Potato Eaters | KY | 67 |
+------+------+-----------+-------+-------+------+------+--------------------+-------+-------+
2 rows in set (0.00 sec)
+----------+--------------------+-------------+---------------+
| painter | number of painting | total price | average price |
+----------+--------------------+-------------+---------------+
| Da Vince | 2 | 121 | 60.5000 |
| Monet | 1 | NULL | NULL |
| Pcasso | 1 | NULL | NULL |
| Renoir | 1 | 64 | 64.0000 |
| Van Gogh | 3 | 148 | 49.3333 |
+----------+--------------------+-------------+---------------+
5 rows in set (0.16 sec)
mysql> select artist.name 'painter' ,count(painting.a_id)'number of painting',sum(painting.price)'total price',avg(painting.price) 'average price' from artist left-> join painting on artist.a_id = painting.a_id group by artist.name;+----------+--------------------+-------------+---------------+| painter | number of painting | total price | average price |+----------+--------------------+-------------+---------------+| Da Vince | 2 | 121 | 60.5000 || Monet | 0 | NULL | NULL || Pcasso | 0 | NULL | NULL || Renoir | 1 | 64 | 64.0000 || Van Gogh | 3 | 148 | 49.3333 |+----------+--------------------+-------------+---------------+
mysql> set @max_val := (select max(painting.price) from painting);Query OK, 0 rows affected (0.00 secmysql> select artist.name,painting.title,painting.price from artist inner join painting on artist.a_id = painting.a_id-> where painting.price = @max_val;+----------+---------------+-------+| name | title | price |+----------+---------------+-------+| Da Vince | Teh Mona Lisa | 87 |+----------+---------------+-------+
mysql> select artist.name,painting.title,painting.price from artist inner join painting-> on artist.a_id = painting.a_id-> where painting.price = (select max(painting.price) from painting);+----------+---------------+-------+| name | title | price |+----------+---------------+-------+| Da Vince | Teh Mona Lisa | 87 |+----------+---------------+-------+1 row in set (0.00 sec)
mysql> select artist.name,painting.title,painting.price-> from artist inner join painting inner join-> (select a_id, max(painting.price) max_price from painting group by a_id) as temp-> on artist.a_id = painting.a_id-> and painting.a_id = temp.a_id-> and painting.price = temp.max_price;+----------+--------------------+-------+| name | title | price |+----------+--------------------+-------+| Da Vince | Teh Mona Lisa | 87 || Van Gogh | The Potato Eaters | 67 || Renoir | Last Deux Soeurs | 64 |+----------+--------------------+-------+3 rows in set (0.63 sec)
mysql> select p1.a_id , p1.title,p1.price from painting p1 left join painting p2on p1.a_id = p2.a_id and p1.price< p2.price-> where p2.a_id is null;+------+--------------------+-------+| a_id | title | price |+------+--------------------+-------+| 1 | Teh Mona Lisa | 87 || 3 | The Potato Eaters | 67 || 5 | Last Deux Soeurs | 64 |+------+--------------------+-------+3 rows in set (0.89 sec)
mysql> select artist.name,temp.title,temp.price from artist inner join-> (select p1.a_id,p1.title,p1.price from painting p1 left join painting p2-> on p1.a_id = p2.a_id-> and p1.price < p2.price-> where p2.a_id is null) as temp-> on artist.a_id = temp.a_id;+----------+--------------------+-------+| name | title | price |+----------+--------------------+-------+| Da Vince | Teh Mona Lisa | 87 || Van Gogh | The Potato Eaters | 67 || Renoir | Last Deux Soeurs | 64 |+----------+--------------------+-------+3 rows in set (0.00 sec)mysql>
+------+----------+-------+------+--------+| half | division | team | wins | losses |+------+----------+-------+------+--------+| 1 | EASTERN | ateam | 24 | 18 || 1 | EASTERN | bteam | 18 | 24 || 1 | EASTERN | cteam | 17 | 24 || 1 | EASTERN | dteam | 15 | 27 || 1 | WESTERN | eteam | 29 | 12 || 1 | WESTERN | fteam | 28 | 14 || 1 | WESTERN | hteam | 21 | 21 || 1 | WESTERN | iteam | 15 | 27 || 2 | EASTERN | jteam | 22 | 20 || 2 | EASTERN | kteam | 21 | 21 || 2 | EASTERN | lteam | 19 | 23 || 2 | EASTERN | mteam | 18 | 24 || 2 | WESTERN | nteam | 26 | 16 || 2 | WESTERN | oteam | 24 | 18 || 2 | WESTERN | pteam | 22 | 20 || 2 | WESTERN | qteam | 16 | 26 |+------+----------+-------+------+--------+16 rows in set (0.00 sec)
mysql> select * ,truncate(wl.wins/(wl.wins + wl.losses),3) as pct,-> if(tm.wl_diff = wl.wins - wl.losses,'-',truncate((tm.wl_diff-(wl.wins-wl.losses))/2,1)) as gb-> from temp_max tm inner join standing wl-> on tm.half = wl.half-> and tm.division = wl.division-> order by wl.half,wl.division,(wl.wins-wl.losses) desc,pct desc;+------+----------+---------+------+----------+-------+------+--------+-------+------+| half | division | wl_diff | half | division | team | wins | losses | pct | gb |+------+----------+---------+------+----------+-------+------+--------+-------+------+| 1 | EASTERN | 6 | 1 | EASTERN | ateam | 24 | 18 | 0.571 | - || 1 | EASTERN | 6 | 1 | EASTERN | bteam | 18 | 24 | 0.428 | 6.0 || 1 | EASTERN | 6 | 1 | EASTERN | cteam | 17 | 24 | 0.414 | 6.5 || 1 | EASTERN | 6 | 1 | EASTERN | dteam | 15 | 27 | 0.357 | 9.0 || 1 | WESTERN | 17 | 1 | WESTERN | eteam | 29 | 12 | 0.707 | - || 1 | WESTERN | 17 | 1 | WESTERN | fteam | 28 | 14 | 0.666 | 1.5 || 1 | WESTERN | 17 | 1 | WESTERN | hteam | 21 | 21 | 0.500 | 8.5 || 1 | WESTERN | 17 | 1 | WESTERN | iteam | 15 | 27 | 0.357 | 14.5 || 2 | EASTERN | 2 | 2 | EASTERN | jteam | 22 | 20 | 0.523 | - || 2 | EASTERN | 2 | 2 | EASTERN | kteam | 21 | 21 | 0.500 | 1.0 || 2 | EASTERN | 2 | 2 | EASTERN | lteam | 19 | 23 | 0.452 | 3.0 || 2 | EASTERN | 2 | 2 | EASTERN | mteam | 18 | 24 | 0.428 | 4.0 || 2 | WESTERN | 10 | 2 | WESTERN | nteam | 26 | 16 | 0.619 | - || 2 | WESTERN | 10 | 2 | WESTERN | oteam | 24 | 18 | 0.571 | 2.0 || 2 | WESTERN | 10 | 2 | WESTERN | pteam | 22 | 20 | 0.523 | 4.0 || 2 | WESTERN | 10 | 2 | WESTERN | qteam | 16 | 26 | 0.380 | 10.0 |+------+----------+---------+------+----------+-------+------+--------+-------+------+16 rows in set (0.00 sec)
mysql> select * from email;+---------------------+---------+---------+---------+---------+-------+| t | srcuser | srchost | dstuser | dsthost | size |+---------------------+---------+---------+---------+---------+-------+| 2014-12-10 01:00:00 | aa | aaa | aaaa | aaaaa | 11111 || 2014-12-10 02:00:00 | bb | bbb | bbbb | bbbbb | 22222 || 2014-12-10 03:00:00 | cc | ccc | cccc | ccccc | 33333 || 2014-12-10 04:00:00 | dd | ddd | dddd | ddddd | 44444 || 2014-12-10 05:00:00 | ee | eee | eeee | eeeee | 55555 || 2014-12-10 05:00:00 | ff | fff | ffff | fffff | 66666 |+---------------------+---------+---------+---------+---------+-------+6 rows in set (0.00 sec)
mysql> select * from ref;+------+| h |+------+| 1 || 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 21 || 22 || 23 || 24 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 |+------+24 rows in set (0.00 sec)
mysql>
通过连接补全列表:mysql> select h ,count(hour(t)) from ref left joinemail on ref.h = hour(email.t) group by h+------+----------------+| h | count(hour(t)) |+------+----------------+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 1 || 5 | 2 || 6 | 0 || 7 | 0 || 8 | 0 || 9 | 0 || 10 | 0 || 11 | 0 || 12 | 0 || 13 | 0 || 14 | 0 || 15 | 0 || 16 | 0 || 17 | 0 || 18 | 0 || 19 | 0 || 20 | 0 || 21 | 0 || 22 | 0 || 23 | 0 || 24 | 0 |+------+----------------+24 rows in set (0.00 sec)mysql>发现数据集中的缺口:mysql> select h from ref left join email on ref.h=hour(email.t) wher e email.t is null;+------+| h |+------+| 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 21 || 22 || 23 || 24 || 6 || 7 || 8 || 9 || 10 || 11 |+------+19 rows in set (0.00 sec)
-> from player_state p1 inner join player_state p2
-> on p1.id +1 = p2.id
-> order by p1.id;
+----+------------+------+------+----+------------+------+------+------+------+-------+
| id | date | ab | h | id | date | ab | h | p_ab | p_h | ba |
+----+------------+------+------+----+------------+------+------+------+------+-------+
| 1 | 2014-12-11 | 0 | 0 | 2 | 2014-12-12 | 38 | 13 | 38 | 13 | 0.342 |
| 2 | 2014-12-12 | 38 | 13 | 3 | 2014-12-13 | 109 | 31 | 71 | 18 | 0.253 |
| 3 | 2014-12-13 | 109 | 31 | 4 | 2014-12-14 | 196 | 49 | 87 | 18 | 0.206 |
| 4 | 2014-12-14 | 196 | 49 | 5 | 2014-12-15 | 304 | 98 | 108 | 49 | 0.453 |
+----+------------+------+------+----+------------+------+------+------+------+-------+
+------------+--------+| date | precip |+------------+--------+| 2014-12-11 | 12 || 2014-12-12 | 10 || 2014-12-13 | 5 || 2014-12-14 | 15 || 2014-12-15 | 16 |+------------+--------+
mysql> select r1.date ,sum(r1.precip) from rainfall r1 inner join rainfall r2-> on r1.date >=r2.date-> group by r1.date;+------------+----------------+| date | sum(r1.precip) |+------------+----------------+| 2014-12-11 | 12 || 2014-12-12 | 20 || 2014-12-13 | 15 || 2014-12-14 | 60 || 2014-12-15 | 80 |+------------+----------------+5 rows in set (0.00 sec)
+------------+--------+| date | precip |+------------+--------+| 2014-12-11 | 12 || 2014-12-13 | 5 || 2014-12-15 | 16 |+------------+--------+3 rows in set (0.00 sec)
+------------+--------+| date | precip |+------------+--------+| 2014-12-11 | 12 || 2014-12-13 | 5 || 2014-12-15 | 16 |+------------+--------+3 rows in set (0.06 sec)
mysql> select * from sales_region; +-----------+------+ | region_id | name | +-----------+------+ | 1 | aaaa | | 2 | bbbb | | 3 | cccc | | 4 | dddd | +-----------+------+ |
mysql> select * from sales_volume; +-----------+------+---------+--------+ | region_id | year | quarter | volume | +-----------+------+---------+--------+ | 1 | 2006 | 1 | 100400 | | 1 | 2006 | 2 | 120000 | | 3 | 2006 | 1 | 280000 | | 3 | 2006 | 2 | 250000 | | 5 | 2006 | 1 | 18000 | | 5 | 2006 | 2 | 32000 | +-----------+------+---------+--------+ 6 rows in set (0.00 sec) |
mysql> select * from sales_region sr left join sales_volume svon sr.region_id = sv.region_id -> where sv.region_id is null; +-----------+------+-----------+------+---------+--------+ | region_id | name | region_id | year | quarter | volume | +-----------+------+-----------+------+---------+--------+ | 2 | bbbb | NULL | NULL | NULL | NULL | | 4 | dddd | NULL | NULL | NULL | NULL | +-----------+------+-----------+------+---------+--------+ 2 rows in set (0.00 sec) |
mysql> select * from sales_region sr left join sales_volume sv on sr.region_id = sv.region_id -> where sv.region_id is null; +-----------+------+-----------+------+---------+--------+ | region_id | name | region_id | year | quarter | volume | +-----------+------+-----------+------+---------+--------+ | 2 | bbbb | NULL | NULL | NULL | NULL | | 4 | dddd | NULL | NULL | NULL | NULL | +-----------+------+-----------+------+---------+--------+ 2 rows in set (0.00 sec) |
删除的方式有两中,其一://叫作多表删除。 mysql>delete sales_volume from sales_volume svleft join sales_region sr -> on sv.region_id = sr.region_id -> where sr.region_id is null; ERROR 1109 (42S02): Unknown table 'sales_volume' in MULTI DELETE //上面的信息表名假如被重命名了就不能在使用原来的名字 mysql>delete sv from sales_volume svleft join sales_region sr -> on sv.region_id = sr.region_id -> where sr.region_id is null; Query OK, 2 rows affected (0.06 sec) mysql> |
删除的方式有两中,其一://使用NOT IN 子查询来识别和删除不匹配的行。
mysql> delete from sales_region where region_id not in(select region_id from sales_volume );
Query OK, 2 rows affected (2.36 sec)
mysql> delete sr from sales_region sr where sr.region_id not in(select region_id from sales_volume sv);
Query OK, 1 row affected (0.08 sec) mysql>
//对于下面的这条子句:mysql> delete from
sales_region as srwhere sr.region_id not in(select region_id from
sales_volume sv);
会报错,报的是an error in your SQL syntax;语法错误;在红色标注的地方
|
painting CREATE TABLE `painting` (`a_id` int(10) unsigned NOT NULL,`p_id` int(10) unsigned NOT NULL AUTO_INCREMENT,`title` varchar(30) NOT NULL,`state` varchar(30) NOT NULL,`price` int(10) unsigned DEFAULT NULL,PRIMARY KEY (`p_id`),KEY `a_id` (`a_id`)) ENGINE=FEDERATED CONNECTION='mysql://username:password@hostname/dbname/tabname'