Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sql server 2005

Sql server 2005

Scheduled Pinned Locked Moved Database
helptutorialdatabasesql-serversysadmin
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nath

    I had two tables, for Table 1 fields are contactId,Str_name and for table 2 contactId,str_phonenumber,Str_phonetype example : Table 1: 111 Krishna 112 Ramesh 113 Kishore Table 2: 111 8389368936398 M1 111 3535353535353 M2 111 6326326326262 L1 111 3263262626326 L2 Now i need like this 111 Krishna 8389368936398 6326326326262 how to join this I just need any of M1 or M2 and L1 or L2 and contactid and Name how to get this. Please anyone can help me i have been strucked with this problem Thanks

    W Offline
    W Offline
    Wendelius
    wrote on last edited by
    #3

    You can join the same table several times. Just use different aliases. Something like:

    select table1.contactId,
    table1.Str_name,
    alias1.str_phonenumber,
    alias2.str_phonenumber
    from table1,
    table2 alias1,
    table2 alias2
    where alias1.contactId = table1.contactId
    and alias2.contactId = table1.contactId
    and alias1.Str_phonetype = 'M1'
    and alias2.Str_phonetype = 'L1'

    Most likely you want to use outer join if there are no matching rows in table2.

    The need to optimize rises from a bad design.My articles[^]

    N 3 Replies Last reply
    0
    • W Wendelius

      You can join the same table several times. Just use different aliases. Something like:

      select table1.contactId,
      table1.Str_name,
      alias1.str_phonenumber,
      alias2.str_phonenumber
      from table1,
      table2 alias1,
      table2 alias2
      where alias1.contactId = table1.contactId
      and alias2.contactId = table1.contactId
      and alias1.Str_phonetype = 'M1'
      and alias2.Str_phonetype = 'L1'

      Most likely you want to use outer join if there are no matching rows in table2.

      The need to optimize rises from a bad design.My articles[^]

      N Offline
      N Offline
      Nath
      wrote on last edited by
      #4

      Thank u very much for ur suggestion which it works

      W 1 Reply Last reply
      0
      • N Nath

        Thank u very much for ur suggestion which it works

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #5

        You're welcome :)

        The need to optimize rises from a bad design.My articles[^]

        1 Reply Last reply
        0
        • W Wendelius

          You can join the same table several times. Just use different aliases. Something like:

          select table1.contactId,
          table1.Str_name,
          alias1.str_phonenumber,
          alias2.str_phonenumber
          from table1,
          table2 alias1,
          table2 alias2
          where alias1.contactId = table1.contactId
          and alias2.contactId = table1.contactId
          and alias1.Str_phonetype = 'M1'
          and alias2.Str_phonetype = 'L1'

          Most likely you want to use outer join if there are no matching rows in table2.

          The need to optimize rises from a bad design.My articles[^]

          N Offline
          N Offline
          Nath
          wrote on last edited by
          #6

          sir when there is no matching rows in table2, i have to retrieve the names from table1 where str_phonenumber are null. I tried with outer join getting errors please suggest me how to do Thanks for Your Help

          W 1 Reply Last reply
          0
          • N Nath

            sir when there is no matching rows in table2, i have to retrieve the names from table1 where str_phonenumber are null. I tried with outer join getting errors please suggest me how to do Thanks for Your Help

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #7

            Nath wrote:

            I tried with outer join getting errors please suggest me how to do

            What is the query like, could you post it? Also what's the error?

            The need to optimize rises from a bad design.My articles[^]

            N 1 Reply Last reply
            0
            • W Wendelius

              Nath wrote:

              I tried with outer join getting errors please suggest me how to do

              What is the query like, could you post it? Also what's the error?

              The need to optimize rises from a bad design.My articles[^]

              N Offline
              N Offline
              Nath
              wrote on last edited by
              #8

              error was cleared but is not displaying record which str_phonenumber values are null

              W 1 Reply Last reply
              0
              • W Wendelius

                You can join the same table several times. Just use different aliases. Something like:

                select table1.contactId,
                table1.Str_name,
                alias1.str_phonenumber,
                alias2.str_phonenumber
                from table1,
                table2 alias1,
                table2 alias2
                where alias1.contactId = table1.contactId
                and alias2.contactId = table1.contactId
                and alias1.Str_phonetype = 'M1'
                and alias2.Str_phonetype = 'L1'

                Most likely you want to use outer join if there are no matching rows in table2.

                The need to optimize rises from a bad design.My articles[^]

                N Offline
                N Offline
                Nath
                wrote on last edited by
                #9

                by this query i am getting records with the phone numbers, but if any one phonenumber is null then that record was not displaying, if the table2 contains no data it should display records like left outer join is it possible to do joins to the above query given by you. could you please help me regarding to this as i was new to this database

                W 1 Reply Last reply
                0
                • N Nath

                  error was cleared but is not displaying record which str_phonenumber values are null

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #10

                  I don't see why str_phonenumber would affect since it wasn't part of the join. Without seeing your query, it's quite impossible to say what's wrong with it.

                  The need to optimize rises from a bad design.My articles[^]

                  1 Reply Last reply
                  0
                  • N Nath

                    by this query i am getting records with the phone numbers, but if any one phonenumber is null then that record was not displaying, if the table2 contains no data it should display records like left outer join is it possible to do joins to the above query given by you. could you please help me regarding to this as i was new to this database

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #11

                    Nath wrote:

                    the table2 contains no data it should display records like left outer join is it possible to do joins to the above query given by you

                    Yes, it's possible. I wrote the query for you so that you can get to start. I'm asking you to post the modified query you have written (the one which has problems) simply because I want to see that you have put effort into this. It doesn't matter if your query isn't working and has problems. The main thing is that you've tried.

                    The need to optimize rises from a bad design.My articles[^]

                    N 1 Reply Last reply
                    0
                    • W Wendelius

                      Nath wrote:

                      the table2 contains no data it should display records like left outer join is it possible to do joins to the above query given by you

                      Yes, it's possible. I wrote the query for you so that you can get to start. I'm asking you to post the modified query you have written (the one which has problems) simply because I want to see that you have put effort into this. It doesn't matter if your query isn't working and has problems. The main thing is that you've tried.

                      The need to optimize rises from a bad design.My articles[^]

                      N Offline
                      N Offline
                      Nath
                      wrote on last edited by
                      #12

                      select t.str_firstname,t.str_lastname,l.str_phonetype,l.str_phonenumber, m.str_phonetype,m.str_phonenumber from trans_contacts t outer join trans_address_phones l,trans_address_phones m where t.int_contactid=l.int_contactid and t.int_contactid=m.int_contactid and l.str_phonetype='L1' and m.str_phonetype='M1' error is incorrect near the join and I tried with single alias also select t.str_firstname,t.str_lastname,l.str_phonetype,l.str_phonenumber from --m.str_phonetype,m.str_phonenumber from trans_contacts t outer join trans_address_phones l on t.int_contactid=l.int_contactid where l.str_phonetype='L1' here is the same problem thats the reason, whether it is possible to work with join, Help me sir

                      W 1 Reply Last reply
                      0
                      • N Nath

                        select t.str_firstname,t.str_lastname,l.str_phonetype,l.str_phonenumber, m.str_phonetype,m.str_phonenumber from trans_contacts t outer join trans_address_phones l,trans_address_phones m where t.int_contactid=l.int_contactid and t.int_contactid=m.int_contactid and l.str_phonetype='L1' and m.str_phonetype='M1' error is incorrect near the join and I tried with single alias also select t.str_firstname,t.str_lastname,l.str_phonetype,l.str_phonenumber from --m.str_phonetype,m.str_phonenumber from trans_contacts t outer join trans_address_phones l on t.int_contactid=l.int_contactid where l.str_phonetype='L1' here is the same problem thats the reason, whether it is possible to work with join, Help me sir

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #13

                        You're on the right track. If you modify the query like the following, do you get right results

                        select t.str_firstname,
                        t.str_lastname,
                        l.str_phonetype,
                        l.str_phonenumber
                        from trans_contacts t
                        left outer join
                        trans_address_phones l
                        on ( t.int_contactid = l.int_contactid
                        and l.str_phonetype = 'L1')

                        The need to optimize rises from a bad design.My articles[^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups