简体   繁体   中英

SQL Cross Join Query not working with variable

This cross join works fine:

select * from x, y

I am trying to run this query:

select abc.col1 from abc
(select * from x, y) abc

But I get this error message:

Msg 8156, Level 16, State 1, Line 2
The column 'col1' was specified multiple times for 'abc'.

Both tables x and y have the same columns and column definitions.

Any ideas/suggestions?

select abc.col1 from abc
(select * from x, y) abc

You are aliasing two tables with the same name. Try:

select abc.col1 from abc,
(select x.* from x, y) abc2

you have to specify column name in inner query section. something like this:

select abc.col1 from abc
(select x.col1,y.col1 from x, y) abc 

In addition to Lock's answer, you also forgot the comma:

select 
    abc.col1 
from abc, (select * from x, y) abc2

Even better, using the ansi 1992 notation:

select 
    abc.col1 
from abc CROSS JOIN (select * from x, y) abc2

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