选择查询:from子句有多个表,但我希望where子句只有两个表的条​​件

时间:2013-04-15 11:57:40

标签: sql select where

例如:

SELECT aa.name, aa.id
    FROM ba_acct_memo aa, acct_mast bb
    WHERE (aa.cod_acct_no=bb.cod_acct_no AND aa.flg_mnt_status='U' 
      and bb.flg_mnt_status='A')

给出1行(例如)

SELECT aa.name, aa.id
    FROM ba_acct_memo aa, acct_mast bb, college cc
    WHERE (aa.cod_acct_no=bb.cod_acct_no AND aa.flg_mnt_status='U'
      and bb.flg_mnt_status='A')

给出与第一个查询相同的结果,但给出它,就像在大学表中那样有多少行。

但它应该像第一次查询一样工作。

有人可以解释我查询的奇怪行为吗?

1 个答案:

答案 0 :(得分:0)

以下步骤将帮助您了解(内部)加入。

            --Create Table 1 :
            Create Table #Table1 
            (
                Roll_No INT ,
                Student_Name Varchar(50)
            )
            Go

            --Create Table 2 :
            Create Table #Table2
            (
                Roll_No INT, 
                Student_Address Varchar(200)
            )
            Go

            --Create Table 3 :
            Create Table #Table3
            (
                Roll_No INT, 
                 Student_Contact_No Varchar(10) 
            )
            Go

            -- Insert Values into #Table1: 
            Insert into #Table1 Values ('1','John')
            Insert into #Table1 Values ('2','Alex')
            Insert into #Table1 Values ('3','Mike')
            Insert into #Table1 Values ('4','Steve')
            Insert into #Table1 Values ('5','Jack')
            Insert into #Table1 Values ('6','Vicky')
            Insert into #Table1 Values ('7','Sid')
            Insert into #Table1 Values ('8','Tom')

            -- Insert Values into #Table2: 
            Insert into #Table2 Values ('1','1st Street')
            Insert into #Table2 Values ('2','2rd Street')
            Insert into #Table2 Values ('3','3rd Street')
            Insert into #Table2 Values ('4','4th Street')
            Insert into #Table2 Values ('5','5th Street')
            Insert into #Table2 Values ('6','6th Street')
            Insert into #Table2 Values ('7','7th Street')
            Insert into #Table2 Values ('8','8th Street')

            -- Insert Values into #Table3: 
            Insert into #Table3 Values ('1','123-123')
            Insert into #Table3 Values ('2','123-111')
            Insert into #Table3 Values ('3','123-122')
            Insert into #Table3 Values ('4','123-125')
            Insert into #Table3 Values ('5','123-126')
            Insert into #Table3 Values ('6','123-116')
            Insert into #Table3 Values ('7','123-145')
            Insert into #Table3 Values ('8','123-132')

            --View Data :
            Select * from #Table1

            Select * from #Table2

            Select * from #Table3

            -- First Query :  (With Out Third Table) : 

            Select A.Roll_No,A.Student_Name,B.Student_Address from #Table1 A, #Table2 B 
            Where a.Roll_No = B.Roll_No
            and a.Roll_No = 1

            --It will Retrun One Record. If you add third table in this query with out  join with any other tables ( Table 1 and Table 2 )

            -- Secord Query :

            Select A.Roll_No,A.Student_Name,B.Student_Address from #Table1 A, #Table2 B , #Table3 C
            Where a.Roll_No = B.Roll_No
            and a.Roll_No = 1

            --It will return 8 Same Records. (Because Table 3 having 8 Records) 
            --You can understand when execute why it is return 8 records.

            -- Third Query : 
            Select *  from #Table1 A, #Table2 B , #Table3 C
            Where a.Roll_No = B.Roll_No
            and a.Roll_No = 1

            --Delete some records from #Table 3 :
            Delete #Table3 Where Roll_No > 5

            --Execute again Third Query :

            -- Third Query : 
            Select *  from #Table1 A, #Table2 B , #Table3 C
            Where a.Roll_No = B.Roll_No
            and a.Roll_No = 1

            --Clean Up:
            Drop table #Table1
            Drop table #Table2
            Drop table #Table3
相关问题