Dev Dev - 26 days ago 4
MySQL Question

How to get a query by selecting rows in the TableA with a particular exclusion in TableB

TableA

clientId clientPassword
1 1234
2 1234
3 1234


TableB

clientId clientCode
1 TRN
2 ABC
3 CDE
3 TRN


What would be the query to select TableA.clientPassword with only those clientID which does not have 'TRN' in TableB.clientCode ?

Part of a complex query but simplified to get my question answered.

Answer

This is a basic not exists;

select a.*
from tableA a
where not exists (select 1
                  from tableB b
                  where a.clientId = b.client_id and b.clientcode = 'TRN'
                 );