Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of not exists? 1

Status
Not open for further replies.
I have two tables(A,B), one of them initially empty
I want to do a select from A if B is empty or if B.field1 does not have a specific value or is NULL.
So something like
Code:
SELECT A.field1, A.field2 FROM A JOIN B ON A.field1=B.field1 WHERE [B is empty]
OR B.field2 !=A.field2

How can one do that in mysql?
 
there are a number of methods to achieve what I think you are trying to achieve. the best is a trade off between various mysql query techniques (left outer joins, not exist statements, not in statements, is null etc) dependent on the table size, structure, indices etc.

try this (without sample create and insert statements it is difficult to test)

Code:
SELECT  a.field1, a.field2
FROM    tableA a
WHERE   a.field1 NOT IN
        (
        SELECT  b.field1
        FROM    tableB c
        )
OR      0 = 
        (
        SELECT COUNT(*)
        FROM    tableB c
        WHERE   c.field1 = a.field1 
        AND     c.field2 = a.field2
        )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top