简体   繁体   中英

select same records multiple times with one column value changed in SQL

I have getting same records multiple times with one column value changed using union like this.

Select col1, col2, 'A' as col3
Union
Select col1, col2, 'B' as col3
Union
Select col1, col2, 'C' as col3

I want to know if there is any way to do this in single query instead of writing 3 queries. Can someone please help?

I simple CROSS JOIN with the specified VALUES should do the trick

Select Col1,Col2,Col3
 From  YourTable A
 Cross Join (Values ('A'),('B'),('C') ) B (Col3)
SELECT d.name, x.field1
FROM sys.databases d
, (VALUES('A'), ('B'), ('C')) AS x(field1)

You should look up the cross join syntax but this is an example off how to do it.

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