Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2006
    Posts
    1

    Unanswered: Mysql query with != and arelational lookup table.

    Hello i have a 2 tables.

    table 1: table2:
    A---B-----C-----D----
    1 - 1 ------ 1 - titel1
    1 - 2 ------ 2 - titel2
    1 - 5 ------ 3 - titel3
    2 - 1 ------ 4 - titel4
    2 - 5 ------ 5 - titel5
    3 - 6 ------ 6 - titel6
    ......... ................

    As you can see my first table works as a lookup,,both have primairy keys.
    now i want to select all from table2 where table1.c = table2.b and table1.A != 1

    Table2 is a lookup for an other table with engines, table 1 contains cars.
    not all cars can have all engines, but now i want to select all engines which have no relation with a car. if a car can have 2 engines from a total of 10 engines. i want to select those 8 remaining...how???

    i normal language,,i want to select from table2 everything,,but not when a number from C has a relation with A.
    i know how to do it vice versa,,
    ($sql = "SELECT * FROM tabel2 LEFT JOIN tabel1 ON tabel1.C = tabel1.B WHERE A = $_GET['id']"

    i hope you guys know what i mean.

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    I think I understand what you're trying to do...

    You want to get the items from one table with no related entry in the second table?

    Two methods come to my mind.

    #1
    Code:
    SELECT *
    FROM tbl1
    WHERE tbl1.tbl2id NOT IN (SELECT DISTINCT tbl2id FROM tbl2)
    #2
    Code:
    SELECT *
    FROM tbl1
    LEFT OUTER JOIN tbl2 ON tbl1.tbl2id = tbl2.tbl2id
    WHERE tbl2.tbl2id IS NULL
    Hope that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •