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
 SQL Query

Author  Topic 

sjonkeesse
Starting Member

2 Posts

Posted - 2012-09-27 : 09:38:31
Hi, I am trying to make a query for my new project.

I have 2 tables in my database and I need only results from 1 table.
The first table is calles `movies`. The second one `trash`.
I need the rows of `movies` exept where the `movies`.`id` equals `trash`.`movies_id`.
I have tried this query:
SELECT * FROM `movies`, `trash` WHERE `movies`.`id` != `trash`.`movies_id` (also with GROUP BY `movies`.`movies_id)
But this query is not working. Im getting a lot of results but also the movies that are in the `trash` table.
Also I have search on the internet for a solution but I don't know exactly where to search for.
Is there someone who knows the answere?
Thanks anyway!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-27 : 09:41:10
select * from movies m where not exists(select * from trash t where t.movies_id = m.id)


Too old to Rock'n'Roll too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-27 : 09:53:03
also

select m.*
from movies m
left join trash t
on t.movies_id = m.id
where t.movies_id is null

select *
from movies
where id not in (select movies_id from trash)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sjonkeesse
Starting Member

2 Posts

Posted - 2012-09-27 : 10:43:36
thank you very much! it's working perfectly!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-27 : 10:45:13
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-27 : 10:45:55
welcome


Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -