简体   繁体   中英

How can I create a view where each row in one table is treated as a child of each row in another table?

I am attempting to create a view which that combines two tables to have the form below.

Table A (column FIRST)

FIRST
-----
A
B
C

Table B (column SECOND)

SECOND
-----
1
2
3

Result

FIRST | SECOND
------|-------
A     | 1
A     | 2
A     | 3
B     | 1
B     | 2
B     | 3
C     | 1
C     | 2
C     | 3

I have been struggling to wrap my brain around this one. Something like the below code is what I would like to end up with for my view, to make it similar to my other views, but all the options I've tried haven't worked quite right.

SELECT
    FIRST,
    '' AS SECOND
FROM Table A

UNION ALL

SELECT
    '' AS FIRST,
    SECOND
FROM Table B

You are looking for a cross join :

select a.first, b.second
from a cross join
     b
order by a.first, b.second;

You can use a CROSS JOIN. For example:

CREATE VIEW v AS 
SELECT a.first, b.second
FROM tablea a
CROSS JOIN tableb b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM