Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Joining Values to A Set Of Values

Say I have two tables:
Table 1:

number
1
2
3

and Table 2:

letter
a
b
c
d
e
f
g

how can i produce

number      letter
a              1
a              2
a              3
b              1
b              2
b              3
c              1
c              2
c              3

all the way to g - i would just like to assign one of each value to each specific value of a table

like image 720
euge1220 Avatar asked Dec 30 '25 17:12

euge1220


2 Answers

While the query suggested like:

SELECT number, letter FROM Table1, Table2

will work the same, and even might look simpler - DON'T USE IT. It is an old style syntax outdated more then 20 years ago with the introduction of ANSI-92 standards.

Please use ANSI-JOINS:

SELECT t1.number, t2.letter
FROM Table1 t1
CROSS JOIN Table2 t2

Also get in the habit to always alias and prefix your columns.

like image 116
Nenad Zivkovic Avatar answered Jan 01 '26 15:01

Nenad Zivkovic


Cross join:

select Table1.number, Table2.letter 
from Table1 CROSS JOIN Table2
like image 20
Icarus Avatar answered Jan 01 '26 17:01

Icarus