English 中文(简体)
页: 1 服务器
原标题:SQL Server many-to-many query
  • 时间:2011-11-21 16:49:46
  •  标签:
  • sql-server

I have two tables, let s call them table1 and table2. They both have a column called ID1 and ID2 which are respective PK for each of two tables.

我还有另一个表格,即:table3,其中载有ID1ID2,在这两个表格之间建立了许多到许多关系。

我需要从<代码>表2上查阅所有与<代码>>>> <1中记录无关的记录。

Ex.

  • Table 1 contains a record with ID = 1
  • Table 2 contains two records with ID 1, 2
  • Table 3 contains one record with values 1 - 1

我需要问一下结果2。

谁能建议我走一条道路?

Thanks

最佳回答
SELECT t2.ID2
    FROM table2 t2
    WHERE NOT EXISTS(SELECT NULL
                         FROM table3 t3
                         WHERE t3.ID2 = t2.ID2);

也可以使用LEFT JOIN:

SELECT t2.ID2
    FROM table2 t2
        LEFT JOIN table3 t3
            ON t2.ID2 = t3.ID2
    WHERE t3.ID2 IS NULL;
问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签