Get data from both tables what join should I use












0















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 |









share|improve this question





























    0















    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 |









    share|improve this question



























      0












      0








      0


      1






      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 |









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 22 hours ago







      kn3l

















      asked 23 hours ago









      kn3lkn3l

      1074




      1074






















          2 Answers
          2






          active

          oldest

          votes


















          2














          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






          share|improve this answer










          New contributor




          Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




























            -1














            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;





            share|improve this answer


























            • There is no MySQL tag there.

              – Sami
              19 hours ago











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            2














            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






            share|improve this answer










            New contributor




            Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              2














              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






              share|improve this answer










              New contributor




              Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                2












                2








                2







                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






                share|improve this answer










                New contributor




                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                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







                share|improve this answer










                New contributor




                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited 19 hours ago





















                New contributor




                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 19 hours ago









                SamiSami

                219111




                219111




                New contributor




                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Sami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.

























                    -1














                    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;





                    share|improve this answer


























                    • There is no MySQL tag there.

                      – Sami
                      19 hours ago
















                    -1














                    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;





                    share|improve this answer


























                    • There is no MySQL tag there.

                      – Sami
                      19 hours ago














                    -1












                    -1








                    -1







                    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;





                    share|improve this answer















                    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;






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    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



















                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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

                    الفوسفات في المغرب

                    Four equal circles intersect: What is the area of the small shaded portion and its height

                    جامعة ليفربول