I am trying to fetch out records from left table only those records which are not in right table. I have near about 5000 records in the left table. Similarly, the same thing is need to be done on more than 3-4 tables. I have to find out records from first table that are not in rest of my 3-4 tables. The same primary or foreign key concept is working their.
My first attempt taking two tables fetch out 5,70,000 records from original 5k records. Repeating the record.
SELECT m.* FROM members m, pinnumber p where p.pinmemberid != m.memberid
My second attempt also made my-sql browser hang.
SELECT m.* FROM members m
LEFT JOIN pinnumber p ON p.pinmemberid != m.memberid
LEFT JOIN customer c ON m.memberid != c.memberid
My third attempt is also making a lot of time
SELECT * FROM members m
WHERE 1=1 AND AND not exists ( select 1 from pinnumber p where 1=1 And
p.pinmemberid = m.memberid AND p.pinproductid LIKE '%Remit%')
AND not exists ( select 1 from customer c where 1=1 and c.card_name is not null AND m.memberid = c.memberid )
Please suggest me what to do with this. If I need to put non-equi join on this.
This query will try to join the first table
memberswith tablespinnumberandcustomer:since we are using a
left join, the query will return all rows of members but if the join doesn’t succeedpinmemberidandmemberidwill benull. The rows in which those values arenullare the ones that doesn’t exist in the right tables. You might want to use OR instead of AND.If you use a join with
!=condition, you will get all rows of the first table multiplied for all rows of the second table, except the ones that have the same id.Another way to get the same result is this:
(substitute AND with OR if you wish). NOT IN clause might be a little slower, but it’s easier to understand.