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

扶醉桌前 提交于 2019-12-07 04:37:49

问题


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?


回答1:


Try this:

SELECT csatuan2,csatuan1,nkonversi FROM ms_metriks 
UNION ALL SELECT 'LTR','PCS','1'
UNION ALL SELECT 'PCS','LTR','1'



回答2:


Here is one way you can do it:

SELECT 'LTR' as csatuan1,'PCS' as csatuan2,'1' as nkonversi
UNION
SELECT 'PCS','LTR','1';



回答3:


I dare to add my experince with Oracle. I had there very similar problem, how to select usernames from table and add to result value 'admin':

There was a problem that this query:

select
  username      
from users
union 
  select 'admin' 

returned a error:

Error at Command Line : 5 Column : 17
Error report -
SQL Error: "FROM keyword not found where expected"

As solution I add added 'dummy' from part with the same table:

select
  username     
from users
union 
  select 'admin' 
  from users

And it works.

J.



来源:https://stackoverflow.com/questions/10733010/how-to-use-union-all-with-manual-value-not-from-another-tabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!