Questions tagged [union]

Use this tag only for questions about UNION, a keyword of the SQL language for combining the results of multiple SQL queries. Use [union-all] for the UNION-ALL keyword. Use the tag [unions] for unions in C, C++, and similar languages.

UNION is a keyword of SQL for combining the results of multiple SQL queries. The results are combined and duplicate rows are eliminated (similar to DISTINCT). If UNION ALL is used, the rows are combined but duplicates are not removed.

Use the tag for unions in C, C++, and similar languages. Use for the UNION-ALL keyword.

Reference

5266 questions
72
votes
7 answers

Incorrect usage of UNION and ORDER BY?

how can i use union and order by in mysql ? select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username where _member_facebook._promote_point = 9 ORDER BY RAND() limit 2 UNION ALL select * from…
Yuda Prawira
  • 12,075
  • 10
  • 46
  • 54
71
votes
3 answers

Group by with union mysql select query

(SELECT COUNT(motorbike.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type='motobike' AND owner.`owner_id`=motorbike.`owner_id` AND transport.`type_id`=motorbike.`motorbike_id` GROUP BY…
user1103332
  • 713
  • 1
  • 5
  • 4
71
votes
14 answers

Combine two tables that have no common fields

I want to learn how to combine two db tables which have no fields in common. I've checked UNION but MSDN says : The following are basic rules for combining the result sets of two queries by using UNION: The number and the order of the columns must…
Tarik
  • 79,711
  • 83
  • 236
  • 349
70
votes
1 answer

How to use union all in LINQ?

How to use union all in LINQ TO SQL. I have use the following code for union, then how to use this for union all? List lstTbEmployee = obj.tbEmployees.ToList(); List lstTbEmployee2 = (from a in lstTbEmployee …
Brillian
  • 1,411
  • 2
  • 16
  • 23
65
votes
10 answers

Hibernate Union alternatives

What alternatives do I have to implement a union query using hibernate? I know hibernate does not support union queries at the moment, right now the only way I see to make a union is to use a view table. The other option is to use plain jdbc, but…
Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
64
votes
5 answers

Using union and count(*) together in SQL query

I have a SQL query, looks something like this: select name, count (*) from Results group by name order by name and another, identical which loads from a archive results table, but the fields are the same. select name, count (*) from Archive_Results…
David Božjak
  • 16,887
  • 18
  • 67
  • 98
62
votes
9 answers

UNION with WHERE clause

I'm doing a UNION of two queries on an Oracle database. Both of them have a WHERE clause. Is there a difference in the performance if I do the WHERE after UNIONing the queries compared to performing the UNION after WHERE clause? For example: SELECT…
MNIK
  • 1,581
  • 3
  • 16
  • 22
58
votes
2 answers

Combine two tables for one output

Say I have two tables: KnownHours: ChargeNum CategoryID Month Hours 111111 1 2/1/09 10 111111 1 3/1/09 30 111111 1 4/1/09 50 222222 1 3/1/09 40 111111 2 …
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
57
votes
5 answers

UNION the results of multiple stored procedures

I have a stored procedure I need to call several different times passing in different paramaters each time. I would like to collect the results as a single dataset. Is something like this possible ... exec MyStoredProcedure 1 UNION exec…
webworm
  • 10,587
  • 33
  • 120
  • 217
57
votes
5 answers

SELECT INTO USING UNION QUERY

I want to create a new table in SQL Server with the following query. I am unable to understand why this query doesn't work. Query1: Works SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 Query2: Does not Work. Error: Msg 170, Level 15, State 1,…
Sekhar
  • 5,614
  • 9
  • 38
  • 44
57
votes
4 answers

Quick way to extend a set if we know elements are unique

I am performing multiple iterations of the type: masterSet=masterSet.union(setA) As the set grows the length of time taken to perform these operations is growing (as one would expect, I guess). I expect that the time is taken up checking whether…
Stewart_R
  • 13,764
  • 11
  • 60
  • 106
56
votes
7 answers

Merge two rows in SQL

Assuming I have a table containing the following information: FK | Field1 | Field2 ===================== 3 | ABC | *NULL* 3 | *NULL* | DEF is there a way I can perform a select on the table to get the following FK | Field1 |…
Jason
  • 741
  • 1
  • 7
  • 7
54
votes
2 answers

Couldnt identify equality operator of type json[] when using UNION

I'm trying to perform multiple queries on a single table using a UNION rule I have two tables: project (id, name, pinned BOOLEAN) skills (m2m to projects) I'm looking to first get an array of rows which have pinned set to true and fill up the…
Jayaram
  • 6,276
  • 12
  • 42
  • 78
51
votes
7 answers

A simple way to sum a result from UNION in MySQL

I have a union of three tables (t1, t2, t3). Each rerun exactly the same number of records, first column is id, second amount: 1 10 2 20 3 20 1 30 2 30 3 10 1 20 2 40 3 50 Is there a simple way in SQL to sum it up, i.e. to only get: 1 …
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
51
votes
6 answers

How to do a count on a union query

I have the following query: select distinct profile_id from userprofile_... union select distinct profile_id from productions_... How would I get the count of the total number of results?
David542
  • 104,438
  • 178
  • 489
  • 842