If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Mysql query with != and arelational lookup table.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-06, 13:19
gillis gillis is offline
Registered User
 
Join Date: Jan 2006
Posts: 1
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.
Reply With Quote
  #2 (permalink)  
Old 01-27-06, 21:02
jfulton jfulton is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On