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. Cross Join - Sql Server

Cross Join - Sql Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
5 Posts 5 Posters 4 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.
  • D Offline
    D Offline
    dataminers
    wrote on last edited by
    #1

    I have two table which are TBL_A and TBL_B. TBL_A contain two record, TBL_B have no record. Select ta.Name From TBL_A ta I get two record. But If wrote following query Select ta.Name From TBL_A ta, TBL_B tb No any record get, because SQL Server defaultly use CROSS JOIN like this; Select ta.Name From TBL_A AS ta CROSS JOIN TBL_B AS tb How can I close CROSS JOIN. I want to get record when TBL_B also have no record.. Actually I use "where" statement, but if one table have no record on FROM statement so I did not get result related about CROSS JOIN.

    D M N R 4 Replies Last reply
    0
    • D dataminers

      I have two table which are TBL_A and TBL_B. TBL_A contain two record, TBL_B have no record. Select ta.Name From TBL_A ta I get two record. But If wrote following query Select ta.Name From TBL_A ta, TBL_B tb No any record get, because SQL Server defaultly use CROSS JOIN like this; Select ta.Name From TBL_A AS ta CROSS JOIN TBL_B AS tb How can I close CROSS JOIN. I want to get record when TBL_B also have no record.. Actually I use "where" statement, but if one table have no record on FROM statement so I did not get result related about CROSS JOIN.

      D Offline
      D Offline
      David Skelly
      wrote on last edited by
      #2

      If I understand your question correctly, what you are looking for is an OUTER JOIN: http://msdn.microsoft.com/en-us/library/ms187518.aspx[^]

      1 Reply Last reply
      0
      • D dataminers

        I have two table which are TBL_A and TBL_B. TBL_A contain two record, TBL_B have no record. Select ta.Name From TBL_A ta I get two record. But If wrote following query Select ta.Name From TBL_A ta, TBL_B tb No any record get, because SQL Server defaultly use CROSS JOIN like this; Select ta.Name From TBL_A AS ta CROSS JOIN TBL_B AS tb How can I close CROSS JOIN. I want to get record when TBL_B also have no record.. Actually I use "where" statement, but if one table have no record on FROM statement so I did not get result related about CROSS JOIN.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Take a look at this Visual Representation of SQL Joins[^], it may help you understand what joins to use and how to use them

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • D dataminers

          I have two table which are TBL_A and TBL_B. TBL_A contain two record, TBL_B have no record. Select ta.Name From TBL_A ta I get two record. But If wrote following query Select ta.Name From TBL_A ta, TBL_B tb No any record get, because SQL Server defaultly use CROSS JOIN like this; Select ta.Name From TBL_A AS ta CROSS JOIN TBL_B AS tb How can I close CROSS JOIN. I want to get record when TBL_B also have no record.. Actually I use "where" statement, but if one table have no record on FROM statement so I did not get result related about CROSS JOIN.

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

          This may help

          declare @tblA table(recordA varchar(10))
          declare @tblB table(recordB varchar(10) null)
          insert into @tblA select 'recA1' union all select 'recA2'

          Query:

          select a.* from @tblA a full join @tblB b
          on a.recordA = b.recordB

          :)

          Niladri Biswas

          1 Reply Last reply
          0
          • D dataminers

            I have two table which are TBL_A and TBL_B. TBL_A contain two record, TBL_B have no record. Select ta.Name From TBL_A ta I get two record. But If wrote following query Select ta.Name From TBL_A ta, TBL_B tb No any record get, because SQL Server defaultly use CROSS JOIN like this; Select ta.Name From TBL_A AS ta CROSS JOIN TBL_B AS tb How can I close CROSS JOIN. I want to get record when TBL_B also have no record.. Actually I use "where" statement, but if one table have no record on FROM statement so I did not get result related about CROSS JOIN.

            R Offline
            R Offline
            Resh Tech
            wrote on last edited by
            #5

            Here is the syntax for two tables

            INSERT INTO answers
            (user_id, question_id)
            (SELECT u.id AS user_id, q.id AS question_id FROM users u, questions q);

            To learn more about cross join in SQL check this out. Cross Join in SQL | SQL Cross Join - Scaler Topics[^]

            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