Max
Max

Reputation: 823

Merge two columns from two tables into one

I'm trying to sort by two different columns from two different tables. This is the situtation:

I have 1 table 'shops' with a column called 'shopy', an INT column. The other table is called 'infra' and has a column called 'y', also an INT.

I would like to select these two columns and sort by them both, so I will get a result like this:

       y
----------------
value from shopy
value from shopy
value from y
value from shopy
value from y
value from y
value from shopy
etc.

So that the shopy and y get merged and sorted by the values of them.

My question to you: is this possible?

Upvotes: 1

Views: 11015

Answers (3)

Deepika Janiyani
Deepika Janiyani

Reputation: 1477

 SELECT shopy as y FROM shops
 UNION ALL
 SELECT y FROM infra
 ORDER BY y ASC

for Descending order write Order by y DESC.

Demo at http://sqlfiddle.com/#!2/62884/1

Upvotes: 2

uvais
uvais

Reputation: 416

yes try this :if you want in descending order

SELECT <columnnane> FROM tableName
UNION ALL
SELECT <columnnane> FROM tablename
ORDER BY <columnnane> DESC

:if you want in ascending order:

SELECT <columnnane> FROM tableName
UNION ALL
SELECT <columnnane> FROM tablename
ORDER BY <columnnane>

Upvotes: 0

Lanello
Lanello

Reputation: 59

yes of course is possible and yes of course you can.

all you have to do is a temporary table with a column called whatever you want and insert the values of the 2 tables into the temporary table, in the same column.

at the end you only need to select from the teporary table ordering it as you want.

read the tutorial posted here

Upvotes: 0

Related Questions