Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use union all with manual value (not from another tabel)?

I want to use union all with manual value, not from another table. And the values are:

|cSatuan1|cSatuan2|nkonversi|
=============================
|   LTR  |   PCS  |    1    |
|   PCS  |   LTR  |    1    |

I've made the query with my own way, but it gets error. here is the query:

SELECT csatuan2, csatuan1, nkonversi
FROM ms_metriks union all select 'LTR','PCS','1','PCS','LTR','1'

Can you tell me what's wrong with my query, and what is the right query?

like image 554
blankon91 Avatar asked May 24 '12 07:05

blankon91


People also ask

Can you UNION two different tables?

The SQL UNION operator 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.

How do you UNION all tables in SQL?

The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It does not remove duplicate rows between the various SELECT statements (all rows are returned). Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.


2 Answers

Try this:

SELECT csatuan2,csatuan1,nkonversi FROM ms_metriks 
UNION ALL SELECT 'LTR','PCS','1'
UNION ALL SELECT 'PCS','LTR','1'
like image 150
Marco Avatar answered Oct 09 '22 12:10

Marco


Here is one way you can do it:

SELECT 'LTR' as csatuan1,'PCS' as csatuan2,'1' as nkonversi
UNION
SELECT 'PCS','LTR','1';
like image 23
Sam Choukri Avatar answered Oct 09 '22 13:10

Sam Choukri