选择在连接表中没有相应连接的行

时间:2015-09-15 13:21:19

标签: sql postgresql join

我有两个SQL表 - 客户和小部件。它们之间有一个连接表customers_widgets,它有两列(customer_id和widget_id)

有没有办法可以选择所有未加入小部件的客户?所以他们的id没有出现在连接表的customer_id列中?

3 个答案:

答案 0 :(得分:3)

一般情况下,我发现android { compileSdkVersion 23 buildToolsVersion '23.0.1' defaultConfig { applicationId "" minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.code.gson:gson:2.3.1' compile 'com.android.support:recyclerview-v7:23.0.0' compile 'com.android.support:appcompat-v7:23.0.1' } 费用昂贵且速度慢,但您的里程数可能因不同的RDBMS而异。

我最常使用的两种选择是:

NOT IN

和...

SELECT
    *
FROM
    customer
WHERE
    NOT EXISTS (SELECT *
                  FROM customers_widgets
                 WHERE customers_widgets.customer_id = customer.customer_id 
               )

答案 1 :(得分:0)

试试这个:

SELECT customer_id
FROM customer
WHERE customer_id NOT IN (SELECT customer_id 
                          FROM customers_widgets)

答案 2 :(得分:0)

您可以使用OUTER JOIN

Select    C.*
From      customer         C
Left Join customer_widgets W  On  C.customer_id = W.customer_id
Where     W.customer_id Is Null