从非相关表中选择不同的相同列值

时间:2022-03-01 14:15:29

In my database, I have 3 tables that have 2 similar columns, Year and Month. These tables aren't linked by anything.

在我的数据库中,我有3个表有2个相似的列,年份和月份。这些表没有任何关联。

What i want to do is select the distinct year and months from these tables. So where table 1 contains:

我想要做的是从这些表中选择不同的年份和月份。那么表1包含:

2009 MAY (multiple times) 2008 NOVEMBER (multiple times) 2007 MAY (multiple times)

2009年5月(多次)2008年11月(多次)2007年5月(多次)

and table 2 and 3 contains:

表2和表3包含:

2009 NOVEMBER (multiple times) 2009 MAY (multiple times) 2008 NOVEMBER (multiple times) 2008 MAY (multiple times)

2009年11月(多次)2009年5月(多次)2008年11月(多次)2008年5月(多次)

I want to be able to do a select where it brings back a complete list of years and months from 2009 November to 2007 MAY.

我希望能够在2009年11月至2007年5月期间收回完整的年份和月份清单。

I am struggling to work out the query.

我正在努力解决这个问题。

Cheers

干杯

2 个解决方案

#1


4  

SELECT month, year
  FROM table1
UNION
SELECT month, year
  FROM table2

UNION will automatically return only distinct rows.

UNION将自动仅返回不同的行。

#2


0  

select distinct year, month from table 1
union
select distinct year, month from table 2
order by year, month

The only problem with this is because your month is the alpha representation its not going to sort properly, but im sure oracle has a function to turn the string representation of a month into a numeric, and you can just sort by that instead.

唯一的问题是因为你的月份是alpha表示它不能正确排序,但我确定oracle有一个函数将一个月的字符串表示形式转换为数字,你可以改为排序。

#1


4  

SELECT month, year
  FROM table1
UNION
SELECT month, year
  FROM table2

UNION will automatically return only distinct rows.

UNION将自动仅返回不同的行。

#2


0  

select distinct year, month from table 1
union
select distinct year, month from table 2
order by year, month

The only problem with this is because your month is the alpha representation its not going to sort properly, but im sure oracle has a function to turn the string representation of a month into a numeric, and you can just sort by that instead.

唯一的问题是因为你的月份是alpha表示它不能正确排序,但我确定oracle有一个函数将一个月的字符串表示形式转换为数字,你可以改为排序。