sql - Compare records in database where one column exists but another doesn't -


i'm trying compare 2 "lists" in same table , records customerid exists storeid doesn't exist customerid.

lists (table definition)

name             listid   storeid   customerid  baselist           1        10         100 baselist           1        11         100 baselist           1        11         102   newlist            2        11         100 newlist            2        12         102 newlist            2        12         103 

query:

select      newlist.*      lists newlist left join      lists baselist on baselist.customerid = newlist.customerid       baselist.listid = 1     , newlist.listid = 2     , newlist.storeid <> baselist.storeid     , not exists (select 1                      lists c                     baselist.customerid = c.customerid                       , baselist.storeid = c.storeid                       , c.listid = 2) 

current result:

newlist 2 11 100  newlist 2 12 102  

but i'm expecting result

newlist 2 12 102  

as customerid 100 storeid 11 exists.

fiddle

if table definition contains column name (as said), statement below returns result.

i didn't understand select statement.

select * @table name = 'newlist'   , customerid in (select customerid @table name = 'baselist')   , storeid not in (select storeid @table name = 'baselist') 

Comments