Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Results from two queries and output as a single table

I have two queries that I have to run, I cannon join them But their resultant tables have the same structrure.

For example I have

select * from products where producttype=magazine

select * from products where producttype = book

I have to combine the result of these two queries, and then output it as one single result. I have to do this inside a stored procedure.

PS These are just examples I provided, i have a complex table structure. The main thing is I cannot join them.

like image 828
tHeSiD Avatar asked Jan 06 '11 19:01

tHeSiD


People also ask

What can you use to combine data from two or more tables into a single result set?

The SQL UNION statement joins together the output of two or more SELECT statements into a single result set. The field names from the tables need not match, but they must be entered in the same order.

How do I append query results 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.

How do I combine data from multiple tables into one table in SQL?

SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.


2 Answers

select * from products where producttype=magazine
union
select * from products where producttype = book
like image 83
Jahan Zinedine Avatar answered Sep 21 '22 08:09

Jahan Zinedine


I think that magazin and book are varchar values and not columns in your table

select * from products where producttype in ('magazine', 'book');
like image 29
bernd_k Avatar answered Sep 17 '22 08:09

bernd_k