Get data from both tables what join should I use
I have T1 as table 1
----------------
field1 | field2
---------------
val1 | val2
I have T2 as table 2
----------------
field1 | field2
---------------
val11 | val22
I want to write a query which can produce result like this
field1 | field2 | T1.T2 |
------------------------|
val11 | val22 | T2 |
val1 | val2 | T1 |
sql-server sql-server-2012 sql-server-2008
add a comment |
I have T1 as table 1
----------------
field1 | field2
---------------
val1 | val2
I have T2 as table 2
----------------
field1 | field2
---------------
val11 | val22
I want to write a query which can produce result like this
field1 | field2 | T1.T2 |
------------------------|
val11 | val22 | T2 |
val1 | val2 | T1 |
sql-server sql-server-2012 sql-server-2008
add a comment |
I have T1 as table 1
----------------
field1 | field2
---------------
val1 | val2
I have T2 as table 2
----------------
field1 | field2
---------------
val11 | val22
I want to write a query which can produce result like this
field1 | field2 | T1.T2 |
------------------------|
val11 | val22 | T2 |
val1 | val2 | T1 |
sql-server sql-server-2012 sql-server-2008
I have T1 as table 1
----------------
field1 | field2
---------------
val1 | val2
I have T2 as table 2
----------------
field1 | field2
---------------
val11 | val22
I want to write a query which can produce result like this
field1 | field2 | T1.T2 |
------------------------|
val11 | val22 | T2 |
val1 | val2 | T1 |
sql-server sql-server-2012 sql-server-2008
sql-server sql-server-2012 sql-server-2008
edited 22 hours ago
kn3l
asked 23 hours ago
kn3lkn3l
1074
1074
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC
as
WITH CTE AS
(
SELECT Col1, Col2, 'T1' [T1.T2]
FROM T1
UNION
SELECT Col1, Col2, 'T2'
FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;
Live Demo
New contributor
add a comment |
Maybe this query is useful... I used union to join the 2 tables.
mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
There is no MySQL tag there.
– Sami
19 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227448%2fget-data-from-both-tables-what-join-should-i-use%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC
as
WITH CTE AS
(
SELECT Col1, Col2, 'T1' [T1.T2]
FROM T1
UNION
SELECT Col1, Col2, 'T2'
FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;
Live Demo
New contributor
add a comment |
Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC
as
WITH CTE AS
(
SELECT Col1, Col2, 'T1' [T1.T2]
FROM T1
UNION
SELECT Col1, Col2, 'T2'
FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;
Live Demo
New contributor
add a comment |
Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC
as
WITH CTE AS
(
SELECT Col1, Col2, 'T1' [T1.T2]
FROM T1
UNION
SELECT Col1, Col2, 'T2'
FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;
Live Demo
New contributor
Simply, you can use a CTE or SubQuery then ORDER BY [T1.T2] DESC
as
WITH CTE AS
(
SELECT Col1, Col2, 'T1' [T1.T2]
FROM T1
UNION
SELECT Col1, Col2, 'T2'
FROM T2
)
SELECT *
FROM CTE
ORDER BY [T1.T2] DESC;
Live Demo
New contributor
edited 19 hours ago
New contributor
answered 19 hours ago
SamiSami
219111
219111
New contributor
New contributor
add a comment |
add a comment |
Maybe this query is useful... I used union to join the 2 tables.
mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
There is no MySQL tag there.
– Sami
19 hours ago
add a comment |
Maybe this query is useful... I used union to join the 2 tables.
mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
There is no MySQL tag there.
– Sami
19 hours ago
add a comment |
Maybe this query is useful... I used union to join the 2 tables.
mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
Maybe this query is useful... I used union to join the 2 tables.
mysql> select field1 ,field2 from T2 union select field1, field2 from T1;
edited 20 hours ago
Jonny
1185
1185
answered 22 hours ago
Ranjith KumarRanjith Kumar
12
12
There is no MySQL tag there.
– Sami
19 hours ago
add a comment |
There is no MySQL tag there.
– Sami
19 hours ago
There is no MySQL tag there.
– Sami
19 hours ago
There is no MySQL tag there.
– Sami
19 hours ago
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227448%2fget-data-from-both-tables-what-join-should-i-use%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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