要合并,请使用简单联接。以下是语法-
select aliasName1.yourColumnName1, aliasName1.yourColumnName2, . . .N aliasName2.yourColumnName1 from yourTableName1 aliasName1 . . . N join yourTableName2 aliasName2 on yourCondition;
让我们创建一个表-
mysql> create table demo8 −> ( −> id int, −> name varchar(20) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo8 values(100,'John'); mysql> insert into demo8 values(101,'Mike'); mysql> insert into demo8 values(102,'Bob');
使用select语句显示表中的记录-
mysql> select *from demo8;
这将产生以下输出-
+------+------+ | id | name | +------+------+ | 100 | John | | 101 | Mike | | 102 | Bob | +------+------+ 3 rows in set (0.00 sec)
以下是创建第二个表的查询-
mysql> create table demo9 −> ( −> id int, −> age int −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo9 values(100,27); mysql> insert into demo9 values(101,24); mysql> insert into demo9 values(102,28);
使用select语句显示表中的记录-
mysql> select *from demo9;
这将产生以下输出-
+------+------+ | id | age | +------+------+ | 100 | 27 | | 101 | 24 | | 102 | 28 | +------+------+ 3 rows in set (0.00 sec)
以下是合并结果的查询-
mysql> select tbl1.id,tbl1.name,tbl2.age −> from demo8 tbl1 −> join −> demo9 tbl2 −> on tbl1.id=tbl2.id;
这将产生以下输出-
+------+------+------+ | id | name | age | +------+------+------+ | 100 | John | 27 | | 101 | Mike | 24 | | 102 | Bob | 28 | +------+------+------+ 3 rows in set (0.00 sec)