Forum Moderators: open

Message Too Old, No Replies

Run COUNT on two tables

show only those records where 2nd table count=0

         

Anyango

8:56 am on Jan 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Lets say we have two tables,

1) impressions (id,keyword,visits)
2) sales (id,keyword,product_id,.......)

I want to grab a list of keywords which are present in impressions but not in sales. we are talking above 50k rows here and i have not been able to do any "Optimized" query for that yet. tried using HAVING with subquery but that almost kills the mysql deamon. Any solution thats executes fast ?

Thanks

Skywarden

10:10 pm on Jan 9, 2009 (gmt 0)

10+ Year Member



This works in MSSQL but I don't have MySQL installed. Give it a shot.

select impressions.keyword from impressions LEFT OUTER JOIN sales on impressions.keyword = sales.keyword where sales.keyword is null

This should return keywords present in impressions but not in sales.

If you want a count of the records it returns, just add the count() function to it:

select count(impressions.keyword) from impressions LEFT OUTER JOIN sales on impressions.keyword = sales.keyword where sales.keyword is null

[edited by: Skywarden at 10:12 pm (utc) on Jan. 9, 2009]