Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Append table queries

Tags:

sql

I have two tables, say table1 with two rows of data say row11 and row12 and table2 with 3 rows of data sat row21, row22, row23

Can anyone provide me with the SQL to create a query that returns

row11
row12
row21
row22
row23

Note: I dont want to create a new table just return the data.

like image 599
Dave Avatar asked Jul 26 '10 02:07

Dave


People also ask

How do I append a query in SQL?

On the Home tab, in the View group, click View, and then click Design View. On the Design tab, in the Query Type group, click Append. The Append dialog box appears. Next, you specify whether to append records to a table in the current database, or to a table in a different database.

Can you append tables in SQL?

SQL has strict rules for appending data: Both tables must have the same number of columns. The columns must have the same data types in the same order as the first table.

What is an append query?

An Append query takes a group of records from one or more tables or queries in your database and adds them to another table. Append queries are especially useful for importing information into a table.


1 Answers

Use UNION ALL, based on the example data:

SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2

UNION removes duplicates - if both tables each had a row whose values were "rowx, 1", the query will return one row, not two. This also makes UNION slower than UNION ALL, because UNION ALL does not remove duplicates. Know your data, and use appropriately.

like image 78
OMG Ponies Avatar answered Nov 03 '22 00:11

OMG Ponies