是的,我们可以做到。语法如下-Syntax1: select * from yourTableName1,yourTableName2;
Syntax2: select * from yourTableName1 cross join yourTableName2;
上面两种语法都给出相同的结果。
让我们首先创建一个表-mysql> create table DemoTable1882
(
Id int
);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable1882 values(10);
mysql> insert into DemoTable1882 values(20);
mysql> insert into DemoTable1882 values(30);
使用select语句显示表中的所有记录-mysql> select * from DemoTable1882;
这将产生以下输出-+------+
| Id |
+------+
| 10 |
| 20 |
| 30 |
+------+
3 rows in set (0.00 sec)
这是创建第二个表的查询-mysql> create table DemoTable1883
(
Id int
);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable1883 values(100);
mysql> insert into DemoTable1883 values(101);
mysql> insert into DemoTable1883 values(102);
使用select语句显示表中的所有记录-mysql> select * from DemoTable1883;
这将产生以下输出-+------+
| Id |
+------+
| 100 |
| 101 |
| 102 |
+------+
3 rows in set (0.00 sec)
这是在SELECT语句之间使用逗号的查询-mysql> select * from DemoTable1882,DemoTable1883;
这将产生以下输出-+------+------+
| Id | Id |
+------+------+
| 10 | 100 |
| 20 | 100 |
| 30 | 100 |
| 10 | 101 |
| 20 | 101 |
| 30 | 101 |
| 10 | 102 |
| 20 | 102 |
| 30 | 102 |
+------+------+
9 rows in set (0.00 sec)
这是第二个获得相同结果的查询,例如SELECT语句之间的逗号-mysql> select * from DemoTable1882 cross join DemoTable1883;
这将产生以下输出-+------+------+
| Id | Id |
+------+------+
| 10 | 100 |
| 20 | 100 |
| 30 | 100 |
| 10 | 101 |
| 20 | 101 |
| 30 | 101 |
| 10 | 102 |
| 20 | 102 |
| 30 | 102 |
+------+------+
9 rows in set (0.00 sec)