How to set column by two tables from side by side in union SQL query





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I want to show two different data from two different table in two side by side column by union SQL query



Let's see an example of it - here is my code:



select CashIN_ID, '' as cashout 
from tbl_CashIN
where CashIN_ID != 0

union

select '', CashOut
from tbl_cashout


This statement shows the following output:



CashIN_ID | cashout
----------+---------
1 | 0
2 | 0
3 | 0
0 | 1
0 | 2
0 | 3


But I don't want this type of output



I want to hide the 0 or can be said that I don't want to show the null value or 0



I want output like this:



CashIN_ID | cashout
----------+--------
1 | 1
2 | 2
3 | 3
4 | null


How can get this output from my SQL query or any other query?










share|improve this question




















  • 1





    Which dbms returns 0 when you select ''?!?

    – jarlh
    Jan 3 at 7:38











  • What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

    – jarlh
    Jan 3 at 7:40











  • As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

    – GauravS
    Jan 3 at 7:42






  • 1





    And if you change the CashIN_ID 2 to 5? What's the expected result?

    – jarlh
    Jan 3 at 7:57


















-1















I want to show two different data from two different table in two side by side column by union SQL query



Let's see an example of it - here is my code:



select CashIN_ID, '' as cashout 
from tbl_CashIN
where CashIN_ID != 0

union

select '', CashOut
from tbl_cashout


This statement shows the following output:



CashIN_ID | cashout
----------+---------
1 | 0
2 | 0
3 | 0
0 | 1
0 | 2
0 | 3


But I don't want this type of output



I want to hide the 0 or can be said that I don't want to show the null value or 0



I want output like this:



CashIN_ID | cashout
----------+--------
1 | 1
2 | 2
3 | 3
4 | null


How can get this output from my SQL query or any other query?










share|improve this question




















  • 1





    Which dbms returns 0 when you select ''?!?

    – jarlh
    Jan 3 at 7:38











  • What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

    – jarlh
    Jan 3 at 7:40











  • As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

    – GauravS
    Jan 3 at 7:42






  • 1





    And if you change the CashIN_ID 2 to 5? What's the expected result?

    – jarlh
    Jan 3 at 7:57














-1












-1








-1








I want to show two different data from two different table in two side by side column by union SQL query



Let's see an example of it - here is my code:



select CashIN_ID, '' as cashout 
from tbl_CashIN
where CashIN_ID != 0

union

select '', CashOut
from tbl_cashout


This statement shows the following output:



CashIN_ID | cashout
----------+---------
1 | 0
2 | 0
3 | 0
0 | 1
0 | 2
0 | 3


But I don't want this type of output



I want to hide the 0 or can be said that I don't want to show the null value or 0



I want output like this:



CashIN_ID | cashout
----------+--------
1 | 1
2 | 2
3 | 3
4 | null


How can get this output from my SQL query or any other query?










share|improve this question
















I want to show two different data from two different table in two side by side column by union SQL query



Let's see an example of it - here is my code:



select CashIN_ID, '' as cashout 
from tbl_CashIN
where CashIN_ID != 0

union

select '', CashOut
from tbl_cashout


This statement shows the following output:



CashIN_ID | cashout
----------+---------
1 | 0
2 | 0
3 | 0
0 | 1
0 | 2
0 | 3


But I don't want this type of output



I want to hide the 0 or can be said that I don't want to show the null value or 0



I want output like this:



CashIN_ID | cashout
----------+--------
1 | 1
2 | 2
3 | 3
4 | null


How can get this output from my SQL query or any other query?







sql union






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 7:57







karan

















asked Jan 3 at 7:36









karankaran

233113




233113








  • 1





    Which dbms returns 0 when you select ''?!?

    – jarlh
    Jan 3 at 7:38











  • What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

    – jarlh
    Jan 3 at 7:40











  • As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

    – GauravS
    Jan 3 at 7:42






  • 1





    And if you change the CashIN_ID 2 to 5? What's the expected result?

    – jarlh
    Jan 3 at 7:57














  • 1





    Which dbms returns 0 when you select ''?!?

    – jarlh
    Jan 3 at 7:38











  • What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

    – jarlh
    Jan 3 at 7:40











  • As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

    – GauravS
    Jan 3 at 7:42






  • 1





    And if you change the CashIN_ID 2 to 5? What's the expected result?

    – jarlh
    Jan 3 at 7:57








1




1





Which dbms returns 0 when you select ''?!?

– jarlh
Jan 3 at 7:38





Which dbms returns 0 when you select ''?!?

– jarlh
Jan 3 at 7:38













What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

– jarlh
Jan 3 at 7:40





What's the expected result if there are 4 rows from one of the queries, and 3 from the other one?

– jarlh
Jan 3 at 7:40













As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

– GauravS
Jan 3 at 7:42





As @jarlh mentioned, be more clear in your question. Look out for join instead of union if you want results like you have mentioned.

– GauravS
Jan 3 at 7:42




1




1





And if you change the CashIN_ID 2 to 5? What's the expected result?

– jarlh
Jan 3 at 7:57





And if you change the CashIN_ID 2 to 5? What's the expected result?

– jarlh
Jan 3 at 7:57












1 Answer
1






active

oldest

votes


















2














For your sample output just you could use join



select t1.CashIN_ID,t2.CashOut
from
tbl_CashIN t1
left join tbl_cashout t2
on t1.CashIN_ID=t2.CashOut





share|improve this answer





















  • 1





    It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

    – Brian Cryer
    Jan 3 at 8:00











  • LEFT join tbl_cashout t2, add left on join to add consider null values

    – Nick
    Jan 3 at 8:06






  • 1





    FULL OUTER JOIN, but I don't think that is what OP wants...

    – jarlh
    Jan 3 at 8:09











  • as per I edit question for what I want output at that thing is not done by join query.

    – karan
    Jan 3 at 8:50











  • @karan use left join i edited answer

    – Zaynul Abadin Tuhin
    Jan 3 at 8:50














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018134%2fhow-to-set-column-by-two-tables-from-side-by-side-in-union-sql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














For your sample output just you could use join



select t1.CashIN_ID,t2.CashOut
from
tbl_CashIN t1
left join tbl_cashout t2
on t1.CashIN_ID=t2.CashOut





share|improve this answer





















  • 1





    It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

    – Brian Cryer
    Jan 3 at 8:00











  • LEFT join tbl_cashout t2, add left on join to add consider null values

    – Nick
    Jan 3 at 8:06






  • 1





    FULL OUTER JOIN, but I don't think that is what OP wants...

    – jarlh
    Jan 3 at 8:09











  • as per I edit question for what I want output at that thing is not done by join query.

    – karan
    Jan 3 at 8:50











  • @karan use left join i edited answer

    – Zaynul Abadin Tuhin
    Jan 3 at 8:50


















2














For your sample output just you could use join



select t1.CashIN_ID,t2.CashOut
from
tbl_CashIN t1
left join tbl_cashout t2
on t1.CashIN_ID=t2.CashOut





share|improve this answer





















  • 1





    It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

    – Brian Cryer
    Jan 3 at 8:00











  • LEFT join tbl_cashout t2, add left on join to add consider null values

    – Nick
    Jan 3 at 8:06






  • 1





    FULL OUTER JOIN, but I don't think that is what OP wants...

    – jarlh
    Jan 3 at 8:09











  • as per I edit question for what I want output at that thing is not done by join query.

    – karan
    Jan 3 at 8:50











  • @karan use left join i edited answer

    – Zaynul Abadin Tuhin
    Jan 3 at 8:50
















2












2








2







For your sample output just you could use join



select t1.CashIN_ID,t2.CashOut
from
tbl_CashIN t1
left join tbl_cashout t2
on t1.CashIN_ID=t2.CashOut





share|improve this answer















For your sample output just you could use join



select t1.CashIN_ID,t2.CashOut
from
tbl_CashIN t1
left join tbl_cashout t2
on t1.CashIN_ID=t2.CashOut






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 8:51

























answered Jan 3 at 7:42









Zaynul Abadin TuhinZaynul Abadin Tuhin

18.9k31135




18.9k31135








  • 1





    It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

    – Brian Cryer
    Jan 3 at 8:00











  • LEFT join tbl_cashout t2, add left on join to add consider null values

    – Nick
    Jan 3 at 8:06






  • 1





    FULL OUTER JOIN, but I don't think that is what OP wants...

    – jarlh
    Jan 3 at 8:09











  • as per I edit question for what I want output at that thing is not done by join query.

    – karan
    Jan 3 at 8:50











  • @karan use left join i edited answer

    – Zaynul Abadin Tuhin
    Jan 3 at 8:50
















  • 1





    It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

    – Brian Cryer
    Jan 3 at 8:00











  • LEFT join tbl_cashout t2, add left on join to add consider null values

    – Nick
    Jan 3 at 8:06






  • 1





    FULL OUTER JOIN, but I don't think that is what OP wants...

    – jarlh
    Jan 3 at 8:09











  • as per I edit question for what I want output at that thing is not done by join query.

    – karan
    Jan 3 at 8:50











  • @karan use left join i edited answer

    – Zaynul Abadin Tuhin
    Jan 3 at 8:50










1




1





It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

– Brian Cryer
Jan 3 at 8:00





It will need to be an outer join (to allow nulls to come through where there are no matching records), but otherwise this is the way to do it.

– Brian Cryer
Jan 3 at 8:00













LEFT join tbl_cashout t2, add left on join to add consider null values

– Nick
Jan 3 at 8:06





LEFT join tbl_cashout t2, add left on join to add consider null values

– Nick
Jan 3 at 8:06




1




1





FULL OUTER JOIN, but I don't think that is what OP wants...

– jarlh
Jan 3 at 8:09





FULL OUTER JOIN, but I don't think that is what OP wants...

– jarlh
Jan 3 at 8:09













as per I edit question for what I want output at that thing is not done by join query.

– karan
Jan 3 at 8:50





as per I edit question for what I want output at that thing is not done by join query.

– karan
Jan 3 at 8:50













@karan use left join i edited answer

– Zaynul Abadin Tuhin
Jan 3 at 8:50







@karan use left join i edited answer

– Zaynul Abadin Tuhin
Jan 3 at 8:50






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018134%2fhow-to-set-column-by-two-tables-from-side-by-side-in-union-sql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The term 'EXEC' is not recognized as the name of a cmdlet Powershell

NPM command prompt closes immediately [closed]

Error binding properties and functions in emscripten