Reply
MySQL Select where not in other table
Old 08-07-2008, 04:53 AM MySQL Select where not in other table
Galaxian's Avatar
Dingleberry!

Posts: 592
Name: Rich
Location: United Kingdom
I'm trying to figure out how to go about this. I want MySQL to select the results where the ID of the result isn't in this certain other table.

PHP Code:
$query mysql_query("select * from links as l inner join links_pro as lp on l.id != lp.link where l.expires > NOW() && l.active = 1 && l.category = '".$category['id']."' order by id desc limit 20"); 
That's the only suggestion I have so far, however it doesn't seem to be functioning correct. I'm presuming it's not that simple? Or maybe I've gone OTT? Suggestions!
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
When You Register, These Ads Go Away!
Old 08-07-2008, 05:35 AM Re: MySQL Select where not in other table
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
in that case, an inner join is not your friend. Try with a "not exists" rather.
Note that as I do't use mysql, I'm not sure it's supported.
But in theory, it would be the way to go:
Code:
select * 
from links as l 
where not exists (
  --this means, that we don't care about the data, we just want to know if the row exists.
  select 1
  from links_pro as lp
  where lp.id=l.id
)
and l.expires > NOW() 
and l.active = 1 
and l.category = '$category['id']' 
order by id desc 
limit 20
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 08-07-2008, 05:38 AM Re: MySQL Select where not in other table
mtishetsky's Avatar
King Spam Talker

Posts: 1,062
Name: Mike
Location: Mataro, Spain
select t1.* from table1 t1 left join table2 t2 on t1.field = t2.field where t2.field is null

This will select all records from table 1 which has no corresponding records in table2, assuming that table1 and table2 are linked through 'field' column.

"t1 join t2 on t1.field1 != t2.field2" will produce a table with M*N rows where M is number of rows in t1 and N is number of rows in t2. This is definitely not what you want.
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is online now
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 08-07-2008, 05:40 AM Re: MySQL Select where not in other table
mtishetsky's Avatar
King Spam Talker

Posts: 1,062
Name: Mike
Location: Mataro, Spain
following tripy's advice keep in mind that subqueries most often work slower than joins, especially on simple queries like yours
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is online now
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 08-07-2008, 05:58 AM Re: MySQL Select where not in other table
Galaxian's Avatar
Dingleberry!

Posts: 592
Name: Rich
Location: United Kingdom
Thanks!

Worked great.
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-07-2008, 07:00 AM Re: MySQL Select where not in other table
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
Quote:
Originally Posted by mtishetsky View Post
following tripy's advice keep in mind that subqueries most often work slower than joins, especially on simple queries like yours
Your solution made me wonder if I did overlooked something.
Checking it, I understood that "left join" is a shortcut for "left outer join", which makes perfect sense.

But on the performance statement, I do think they are mostly equals.
outer join are hard on the query planner too.
At least on pgsql and ms sql server, I see very little differences in term of performances between an outer join and a not exists condition.

I didn't used mysql for several years, so I could not say for sure how the planner handled those 2 cases.
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 08-07-2008 at 07:01 AM.
tripy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL Select where not in other table
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13900 seconds with 12 queries