Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select best rows except those in a second table

Author  Topic 

span
Starting Member

2 Posts

Posted - 2011-01-14 : 14:55:34
Hello,
I have two tables:
1) table containing some ranked data with primary key entryid
2) table containing entries of the form (userid, entryid)

I want to return the best 30 entries from the 1st table that are not in the 2nd.

For example, usually I select rows 1,2,3,4,5 but my user tells me that they don't want to see row 2. Then upon loading the page again, they should see 1,3,4,5,6.

How exactly can I set up a query that does that? My ranking query is something like

SELECT * FROM ENTRYTABLE WHERE foo, ORDER BY `bar` desc limit 30

How can I modify this query to avoid entries in the second table?
Thanks so much.

mzuma
Starting Member

2 Posts

Posted - 2011-01-14 : 16:31:22
You can use NOT IN or MINUS

SELECT * FROM Table 1 WHERE [some expressions] AND entryid NOT IN (SELECT entryid FROM table2) ORDER BY `bar` desc limit 30

check out the following link:
SQL IN
SQL MINUS
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-14 : 16:37:58
You can also do an OUTER JOIN or use a NOT EXISTS clause.
Go to Top of Page

span
Starting Member

2 Posts

Posted - 2011-01-14 : 16:53:19
Perfect - exactly what I needed. Thanks!
Go to Top of Page
   

- Advertisement -