0

I have two tables on two different databases.

I wish to select the values from the field MLS_LISTING_ID from the table mlsdata if they do not exist in the table ft_form_8.

There are a total of 5 records in the mlsdata table. There are 2 matching records in the ft_form_8 table.

Running this query, I receive all 5 records from mlsdata instead of 3.

Changing NOT IN to IN, I get the 2 matching records that are in both tables.

Any ideas?

SELECT DISTINCT
    flrhost_mls.mlsdata.MLS_LISTING_ID
FROM
    flrhost_mls.mlsdata
        INNER JOIN
    flrhost_forms.ft_form_8 ON flrhost_mls.mlsdata.MLS_AGENT_ID = flrhost_forms.ft_form_8.nar_id
WHERE
    flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN ((SELECT flrhost_forms.ft_form_8.mls_id))
        AND flrhost_mls.mlsdata.MLS_AGENT_ID = '260014126'
        AND flrhost_forms.ft_form_8.transaction_type = 'listing' 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
WGS
  • 199
  • 4
  • 17

3 Answers3

2
SELECT DISTINCT
    flrhost_mls.mlsdata.MLS_LISTING_ID
FROM
    flrhost_mls.mlsdata
where
    flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN (SELECT 
            flrhost_forms.ft_form_8.mls_id
        FROM
            flrhost_forms.ft_form_8)
Faishal
  • 1,493
  • 1
  • 14
  • 23
2

Your problem:

I wish to select the values from the field MLS_LISTING_ID from the table mlsdata if they do not exist in the table ft_form_8.

is THE classic OUTER JOIN problem

SELECT DISTINCT
  flrhost_mls.mlsdata.MLS_LISTING_ID, flrhost_forms.ft_form_8.MLS_LISTING_ID
 FROM 
   flrhost_mls.mlsdata
 LEFT OUTER JOIN
   flrhost_forms.ft_form_8
 ON
   flrhost_mls.mlsdata.MLS_LISTING_ID=flrhost_forms.ft_form_8.MLS_LISTING_ID
 WHERE
   flrhost_forms.ft_form_8.MLS_LISTING_ID IS NULL;

Which is much simpler than using some complicated subquery nest.

Mr Purple
  • 2,325
  • 1
  • 18
  • 15
0
SELECT DISTINCT
  flrhost_mls.mlsdata.MLS_LISTING_ID
FROM flrhost_mls.mlsdata
INNER JOIN flrhost_forms.ft_form_8 
ON flrhost_mls.mlsdata.MLS_AGENT_ID = flrhost_forms.ft_form_8.nar_id  
WHERE flrhost_mls.mlsdata.MLS_AGENT_ID = '260014126'
  AND flrhost_forms.ft_form_8.transaction_type = 'listing' 
  AND flrhost_mls.mlsdata.MLS_LISTING_ID NOT IN (SELECT b.mls_id FROM flrhost_forms.ft_form_8 b)
vemcaster
  • 361
  • 4
  • 9