MySQL cartesian product between two SELECT statements

If you specify your tables with out any JOIN ON clause or equalities/conditionins in the WHERE clause you’ll get the catesian product you’re looking for.

SELECT table1.field1, table2.field2
FROM table1, table2

will give you what you’re asking for. Showing it more explicitly…

SELECT * FROM table1;
+--------+
| field1 |
+--------+
|      1 |
|      2 |
+--------+

SELECT * FROM table2;
+--------+
| field2 |
+--------+
|      3 |
|      4 |
+--------+

SELECT table1.field1, table2.field2 FROM table1, table2;
+--------+--------+
| field1 | field2 |
+--------+--------+
|      1 |      3 |
|      2 |      3 |
|      1 |      4 |
|      2 |      4 |
+--------+--------+

Leave a Comment