Reputation: 61
Let's imagine that we have these 2 tables:
Table 1, with the column:
Field1 1 3
Table 2, with the column:
Field1 2 4
(Well they could also be called in any other way, but I want to represent that the type of table1.field1 is the same as table2.field1).
Would it be possible to do a SQL query that would return the following?
[1,2,3,4], I mean the numbers ordered by any criteria I would want but that criteria aplying to both tables. As far as I know ORDER BY can just ORDER by the values of a column, not by a general criteria like "from lower to higher number. And even if it could I believe the SELECT instruction can't fuse columns. I mean I think the best I could achieve with that instruction would be to get something like [(1,2),(1,4),(3,2),(3,4)] and later work on it, but this can be painful with lots of results.
And the application needs fields to be on different tables, I cannot merge them.
Any idea about how to deal with this?
Thanks a lot for your help.
Edit:
Oh, it was much easier than what I thought, with that instruction is not something hard to achieve.
Thank you everyone.
Upvotes: 0
Views: 90
Reputation: 34563
This is what the UNION
statement is for. It lets you combine two SELECT
statements into the same resultset:
SELECT Field1
FROM Table1
UNION ALL
SELECT Field1
FROM Table2
ORDER BY 1
Upvotes: 3
Reputation: 3509
select * from
(
select field1 as field_value from table1
union
select field2 as field_value from table2
)
order by field_value asc
Upvotes: 0
Reputation: 70
can you do union all
Like below:
Select field 1
from
(Select field 1 from Table 1
Union
select field 1 from table 2)
order by field 1
Use union
or Union all
based on your need to repeat elements in both the tables or not.
Upvotes: 0