MySQL连接实例
2012-06-14 19:07
211 查看
mysql> use test Database changed mysql> show tables; Empty set (0.00 sec) mysql> create table person( id smallint unsigned NOT NULL AUTO_INCREMENT, name v archar(30), primary key(id)); Query OK, 0 rows affected (0.12 sec) mysql> create table score(id smallint unsigned NOT NULL, score int); Query OK, 0 rows affected (0.14 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | person | | score | +----------------+ 2 rows in set (0.00 sec) mysql> insert into person(name) values ('zrx'),('genius'),('stupid'); Query OK, 3 rows affected (0.11 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from person; +----+--------+ | id | name | +----+--------+ | 1 | zrx | | 2 | genius | | 3 | stupid | +----+--------+ 3 rows in set (0.00 sec) mysql> insert into score values (1, 9),(2, 100); Query OK, 2 rows affected (0.11 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into score values (4,20); Query OK, 1 row affected (0.11 sec) mysql> insert into score values (1,1); Query OK, 1 row affected (0.11 sec) mysql> select * from score; +----+-------+ | id | score | +----+-------+ | 1 | 9 | | 2 | 100 | | 4 | 20 | | 1 | 1 | +----+-------+ 4 rows in set (0.00 sec) mysql> select * from person inner join score ; +----+--------+----+-------+ | id | name | id | score | +----+--------+----+-------+ | 1 | zrx | 1 | 9 | | 2 | genius | 1 | 9 | | 3 | stupid | 1 | 9 | | 1 | zrx | 2 | 100 | | 2 | genius | 2 | 100 | | 3 | stupid | 2 | 100 | | 1 | zrx | 4 | 20 | | 2 | genius | 4 | 20 | | 3 | stupid | 4 | 20 | | 1 | zrx | 1 | 1 | | 2 | genius | 1 | 1 | | 3 | stupid | 1 | 1 | +----+--------+----+-------+ 12 rows in set (0.00 sec) mysql> select * from person inner join score where person.id=score.id; +----+--------+----+-------+ | id | name | id | score | +----+--------+----+-------+ | 1 | zrx | 1 | 9 | | 2 | genius | 2 | 100 | | 1 | zrx | 1 | 1 | +----+--------+----+-------+ 3 rows in set (0.00 sec) mysql> select * from (person, score); +----+--------+----+-------+ | id | name | id | score | +----+--------+----+-------+ | 1 | zrx | 1 | 9 | | 2 | genius | 1 | 9 | | 3 | stupid | 1 | 9 | | 1 | zrx | 2 | 100 | | 2 | genius | 2 | 100 | | 3 | stupid | 2 | 100 | | 1 | zrx | 4 | 20 | | 2 | genius | 4 | 20 | | 3 | stupid | 4 | 20 | | 1 | zrx | 1 | 1 | | 2 | genius | 1 | 1 | | 3 | stupid | 1 | 1 | +----+--------+----+-------+ 12 rows in set (0.00 sec) mysql> select * from (person, score) using (id); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'using (id)' at line 1 mysql> select * from (person, score) where person.id=score.id; +----+--------+----+-------+ | id | name | id | score | +----+--------+----+-------+ | 1 | zrx | 1 | 9 | | 2 | genius | 2 | 100 | | 1 | zrx | 1 | 1 | +----+--------+----+-------+ 3 rows in set (0.00 sec) mysql> select * from person inner join score using (id); +----+--------+-------+ | id | name | score | +----+--------+-------+ | 1 | zrx | 9 | | 2 | genius | 100 | | 1 | zrx | 1 | +----+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from person natural join score; +----+--------+-------+ | id | name | score | +----+--------+-------+ | 1 | zrx | 9 | | 2 | genius | 100 | | 1 | zrx | 1 | +----+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from person left join score; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 mysql> select * from person left join score where person.id=score.id; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where person.id=score.id' at line 1 mysql> select * from person left join score using(id); +----+--------+-------+ | id | name | score | +----+--------+-------+ | 1 | zrx | 9 | | 1 | zrx | 1 | | 2 | genius | 100 | | 3 | stupid | NULL | +----+--------+-------+ 4 rows in set (0.00 sec) mysql> select * from person left join score on person.id=score.id; +----+--------+------+-------+ | id | name | id | score | +----+--------+------+-------+ | 1 | zrx | 1 | 9 | | 1 | zrx | 1 | 1 | | 2 | genius | 2 | 100 | | 3 | stupid | NULL | NULL | +----+--------+------+-------+ 4 rows in set (0.00 sec) mysql> select * from person right join score on person.id=score.id; +------+--------+----+-------+ | id | name | id | score | +------+--------+----+-------+ | 1 | zrx | 1 | 9 | | 2 | genius | 2 | 100 | | NULL | NULL | 4 | 20 | | 1 | zrx | 1 | 1 | +------+--------+----+-------+ 4 rows in set (0.00 sec) mysql> select * from person full outer join score on person.id=score.id; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join score on person.id=score.id' at line 1 mysql> select * from person full join score on person.id=score.id; ERROR 1054 (42S22): Unknown column 'person.id' in 'on clause' mysql> select * from person full join score using(id); +----+--------+-------+ | id | name | score | +----+--------+-------+ | 1 | zrx | 9 | | 2 | genius | 100 | | 1 | zrx | 1 | +----+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from person full join score on person.id=score.id; ERROR 1054 (42S22): Unknown column 'person.id' in 'on clause' mysql> select * from person p full join score s on p.id=s.id; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'full join score s on p.id=s.id' at line 1 mysql> select * from person full outer join score using(id); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join score using(id)' at line 1 mysql> select * from person left join score using(id) union select * from person right join score using (id); +----+--------+--------+ | id | name | score | +----+--------+--------+ | 1 | zrx | 9 | | 1 | zrx | 1 | | 2 | genius | 100 | | 3 | stupid | NULL | | 1 | 9 | zrx | | 2 | 100 | genius | | 4 | 20 | NULL | | 1 | 1 | zrx | +----+--------+--------+ 8 rows in set (0.00 sec) mysql> select * from person left join score on person.id=score.id union select * from person right join score on person.id=score.id; +------+--------+------+-------+ | id | name | id | score | +------+--------+------+-------+ | 1 | zrx | 1 | 9 | | 1 | zrx | 1 | 1 | | 2 | genius | 2 | 100 | | 3 | stupid | NULL | NULL | | NULL | NULL | 4 | 20 | +------+--------+------+-------+ 5 rows in set (0.00 sec) mysql>MySQL本身不支持full join(全连接),但可以通过union来实现。
相关文章推荐
- ajax 实现 GET POST 异步 同步 检验mysql连接情况简单实例
- python连接mysql实例分享_python
- mysql中的内连接,外连接,全连接,交叉连接实例详解
- php 连接mysql实例代码
- mysql 连接实例及函数简介
- 解耦合 jsp 连接 mysql 的实例代码 jsp连接数据库的最好方法
- C++连接MySQL(二):环境配置和实例 使用api方式
- C#连接操作mysql实例
- php mysql连接数据库实例
- 连接mysql的javabean实例+简单分页
- jdbc连接数据库(mysql)实例
- SAE MYSQL 连接实例(创建一个表)
- MySQL查询优化:用子查询代替非主键连接查询实例介绍
- php连接mysql常规实例
- MySQL中的连接、实例、会话、数据库、线程之间的关系
- python3.6连接MySQL和表的创建与删除实例代码
- mysql设置指定ip远程访问连接实例
- C#连接mysql实例
- maven、hibernate连接mysql实例框架
- Python连接MySQL的实例代码