MYSQL Advanced SELECT Question

Discussion in 'MySQL' started by fourdesign, Jan 26, 2010.

  1. #1
    I have two tables in my database, 'Members' and 'Cars' - The 'Members' table holds general information about a member (i.e. Name, Email, Phone, ect.). The 'Cars' table holds information about cars that members register. A member can register multiple cars to his/her account but does not have to register a car when he/she signs up.

    I am looking for the most efficient way to select all of the members who do not have a car registered. I realize that I can select rows in the 'Cars' table for each member in the 'Members' table but this seems extremely inefficient, especially when dealing with tens of thousands of members.

    Can anyone suggest a better method for doing this? I was looking into various JOINS but have very little experience with this technique so I was unable to find an efficient method.

    Thanks!
     
    fourdesign, Jan 26, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Following might help..

    I have assumed that member_id is common between both the tables. You can otherwise replace the common field there in query. Also make sure to remove A.* with actual field list, as A.* might turn heavier.

    SELECT A.*, B.member_id FROM Members A LEFT JOIN Cars B ON A.member_id = B.member_id WHERE B.member_id IS NULL
     
    mastermunj, Jan 27, 2010 IP
  3. fourdesign

    fourdesign Member

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    Thanks! I'll try this out and get back to you with the results.
     
    fourdesign, Jan 27, 2010 IP