squigglytail
squigglytail

Reputation: 31

mysql join tables without keys

Basic question: How do I join 2 tables to get the following output?

Table1:
"type"
red
blue
big
small

===

Table2:
"object"
cat
person
chair

===

Output:
red cat 
blue cat
big cat
small cat
red person
blue person
big person
small person
red chair
blue chair
big chair
small chair

Upvotes: 2

Views: 2098

Answers (4)

spencer7593
spencer7593

Reputation: 108380

SELECT CONCAT(t1.type,' ',t2.object) as `Output:`
  FROM table1 t1 CROSS JOIN table2 t2

(The keyword CROSS is optional in mysql, but it does serve to document that you intended a cartesian product, which can be useful information for the person who reads this statement, who would normally be expecting to see a join predicate.

If you need the result set returned in a particular order, include an ORDER BY clause. It's a relatively complicated expression to get it sorted in the order shown, but it can be done.

Upvotes: 3

Sebas
Sebas

Reputation: 21522

Cartesian product I guess:

SELECT t1.type, t2.object FROM Table1 t1, Table2 t2

Upvotes: 1

Drewness
Drewness

Reputation: 5072

array_combine(table1, table2);

Here is a link for more information.

Upvotes: 1

asenovm
asenovm

Reputation: 6517

SELECT * FROM Table1 CROSS JOIN Table2

Upvotes: 6

Related Questions