INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Use of not exists?

Use of not exists?

(OP)
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?

RE: Use of not exists?

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
        ) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close