I have a database with two tables (Table1 and Table2). They both have a common column [ColumnA] which is an nvarchar.
How can I select this column from both tables and return it as a single column in my result set?
So I'm looking for something like:
ColumnA in Table1:
a
b
c
ColumnA in Table2:
d
e
f
Result set should be:
a
b
c
d
e
f
SELECT ColumnA FROM Table1 UNION Select ColumnB FROM Table2 ORDER BY 1
Also, if you know the contents of Table1 and Table2 will NEVER overlap, you can use UNION ALL in place of UNION instead. Saves a little bit of resources that way.
-- Kevin Fairchild
Do you care if you get dups or not?
UNION will be slower than UNION ALL because UNION will filter out dups
Use the UNION operator:
SELECT ColumnA FROM Table1
UNION
SELECT ColumnA FROM Table2
The union answer is almost correct, depending on overlapping values:
SELECT distinct ColumnA FROM Table1
UNION
SELECT distinct ColumnA FROM Table2
If 'd' appeared in Table1 or 'c' appeared in Table2 you would have multiple rows with them.
You can use a union select:
Select columnA from table1 union select columnA from table2
SELECT Table1.*, Table2.d, Table2.e, Table2.f
FROM Table1 JOIN Table2 ON Table1.a = Table2.a
Or am I misunderstanding your question?
Edit: It appears I did.
I believe it's:
SELECT columna FROM table1 UNION SELECT columnb FROM table2;
In Oracle (at least) there is UNION and UNION ALL, UNION ALL will return all results from both sets even if there are duplicates, where as UNION will return the distinct results from both sets.
Related
I need solution for this type query.Means this query is not real but I need to fetch data like this:(except join and union)
select a b,c
from (table1 or table2)
where name="rohit";
it's simple to do with union
select *
from t1
where t1.name='a'
union (all)
select *
from t2
where t2.name='a'
If union and join is not allowed you can try select both table and create kind of union via GROUP BY trick. Like this
select *
from t1, t2
where t1.name='a' or t2.name='a'
group by (case t1.name
when 'a' then CONCAT('t1',t1.id)
else CONCAT('t2',t2.id) end case)
Haven't tested though. sqlfiddle.com somehow doesn't work showing errors when I try to execute a query like this
Suppose I have two tables:
Table 1
Col
1
3
4
5
6
9
and Table 2
Col
2
4
6
8
How can I Merge the two tables so I have 1-9 and if a number only appears in one table, the corresponding position in the other table has a null? Thank you.
I'm assuming you want the numbers that actually exist in at least one of the tables, which won't give you a row with 7;
What you're looking for seems to be something like a FULL OUTER JOIN which works in pretty much any RDBMS except MySQL;
SELECT a.col col_a, b.col col_b
FROM Table1 a
FULL OUTER JOIN Table2 b ON a.col = b.col
ORDER BY COALESCE(a.col, b.col);
An SQLfiddle to test with.
Sadly, MySQL does not have FULL OUTER JOIN, so you'll have to do the same operation using a UNION between a LEFT JOIN and a RIGHT JOIN;
SELECT * FROM (
SELECT a.col col_a, b.col col_b
FROM Table1 a
LEFT JOIN Table2 b ON a.col = b.col
UNION
SELECT a.col col_a, b.col col_b
FROM Table1 a
RIGHT JOIN Table2 b ON a.col = b.col
)z
ORDER BY COALESCE(col_a, col_b);
Another SQLfiddle.
If i am not wrong. you need records from both table. (please correct me if i am wrong)
Try following to get data from both tables:
select col from Table1
union
select col from Table2
select col from Table1
union all
select col from Table2
NOTE:
UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.
There is a performance hit when using UNION vs UNION ALL, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates (especially when developing reports).
You can try this one using union:
SELECT * FROM (SELECT col AS value FROM table1
UNION
SELECT col AS value FROM table2)t1
ORDER BY value
try and post your comments:
Thanks
I'm trying to create a query and need to join against something that I can define values in without creating a table.
I'll attempt to describe what I'm trying to do:
table1
-------
fieldA
is joined on fieldA with
table2
-------
(titles for FK in table 1)
Table1 has values outside of what exists in table2
I want to add an additional 'table' to be unioned with table2 and then joined with table 1
Thanks
Sure, you can use a UNION ALL inside a subselect and join with the result of that. Something like this might do the trick:
SELECT *
FROM table1 T1
JOIN (
SELECT titles, stuff
FROM table2
UNION ALL
SELECT 'foo' AS titles, 'foostuff' AS stuff
UNION ALL
SELECT 'bar' AS titles, 'barstuff' AS stuff
) T2
ON T1.id = T2.titles
Note that the columns in the UNION ALL must be of the same type and in the same order. The column names don't have to match though.
Looks like you want to add arbitrary results to your query?
select
id,
titles
from
table1 t1
inner join table2 t2
on t2.titles = t1.titles
union (
(select 100, 'Dogs' from dual)
union
(select 200, 'Pigs' from dual)
union
(select 300, 'Sheep' from dual)
)
That's an oracle flavour, for other RDBMS' there will be an equivalent to dual
If you're using a modern Oracle version, there is an even neater solution
WITH arbitrary_data AS (
SELECT 100 id, 'Dogs' titles FROM DUAL
UNION ALL SELECT 200, 'Pigs' FROM DUAL
UNION ALL SELECT 300, 'Sheep' FROM DUAL
)
SELECT
id,
titles
FROM
table1 t1
inner join table2 t2
on t2.titles = t1.titles
inner join arbitrary_data ad
on ad.titles = t1.titles
I have two tables with the same structure:
id name
1 Merry
2 Mike
and
id name
1 Mike
2 Alis
I need to union second table to first with keeping unique names, so that result is:
id name
1 Merry
2 Mike
3 Alis
Is it possible to do this with MySQL query, without using php script?
This is not a join (set multiplication), this is a union (set addition).
SELECT #r := #r + 1 AS id, name
FROM (
SELECT #r := 0
) vars,
(
SELECT name
FROM table1
UNION
SELECT name
FROM table2
) q
This will select all names from table1 and combine those with all the names from table2 which are not in table1.
(
select *
from table1
)
union
(
select *
from table2 t2
left join table1 t1 on t2.name = t1.name
where t1.id is null
)
Use:
SELECT a.id,
a.name
FROM TABLE_A a
UNION
SELECT b.id,
b.name
FROM TABLE_B b
UNION will remove duplicates.
As commented, it all depends on what your 'id' means, cause in the example, it means nothing.
SELECT DISTINCT(name) FROM t1 JOIN t2 ON something
if you only want the names
SELECT SUM(something), name FROM t1 JOIN t2 ON something GROUP BY name
if you want to do some group by
SELECT DISTINCT(name) FROM t1 JOIN t2 ON t1.id = t2.id
if the id's are the same
SELECT DISTINCT COALESCE(t1.name,t2.name) FROM
mytable t1 LEFT JOIN mytable t2 ON (t1.name=t2.name);
will get you a list of unique names from the 2 tables. If you want them to get new ids (like Alis does in your desired results), that's something else and requires the answers to a couple of questions:
do any of the names need to maintain their previous id. And if they do, which table's id should be preferred?
why do you have 2 tables with the same structure? ie what are you trying to accomplish when you generate the unique name list?
If I run the two statements in batch will they return one table to two to my sqlcommand object with the data merged. What I am trying to do is optimize a search by searching twice, the first time on one set of data and then a second on another. They have the same fields and I’d like to have all the records from both tables show and be added to each other. I need this so that I can sort the data between both sets of data but short of writing a stored procedure I can’t think of a way of doing this.
Eg. Table 1 has columns A and B, Table 2 has these same columns but different data source. I then wan to merge them so that if a only exists in one column it is added to the result set and if both exist it eh tables the column B will be summed between the two.
Please note that this is not the same as a full outer join operation as that does not merge the data.
[EDIT]
Here's what the code looks like:
Select * From
(Select ID,COUNT(*) AS Count From [Table1]) as T1
full outer join
(Select ID,COUNT(*) AS Count From [Table2]) as T2
on t1.ID = T2.ID
Perhaps you're looking for UNION?
IE:
SELECT A, B FROM Table1
UNION
SELECT A, B FROM Table2
Possibly:
select table1.a, table1.b
from table1
where table1.a not in (select a from table2)
union all
select table1.a, table1.b+table2.b as b
from table1
inner join table2 on table1.a = table2.a
edit: perhaps you would benefit from unioning the tables before counting. e.g.
select id, count() as count from
(select id from table1
union all
select id from table2)
I'm not sure if I understand completely but you seem to be asking about a UNION
SELECT A,B
FROM tableX
UNION ALL
SELECT A,B
FROM tableY
To do it, you would go:
SELECT * INTO TABLE3 FROM TABLE1
UNION
SELECT * FROM TABLE2
Provided both tables have the same columns
I think what you are looking for is this, but I am not sure I am understanding your language correctly.
select id, sum(count) as count
from (
select id, count() as count
from table1
union all
select id, count() as count
from table2
) a
group by id