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.
Author |
Topic |
arya6000
Starting Member
3 Posts |
Posted - 2010-12-28 : 03:11:35
|
HelloI have 2 tablestable1 which one of its columns is called 'host' (this table can contain multiple hosts)table2 which has a 'host' column as its primary key.I want to make a query that selects one host from table1 if it does not belong in table2 already.I've tried many things and so far I know I have to SELECT DISTINCT with LIMIT 1, but other than that, not sure how to do it. Is this even possible with the way I have my DB setup?Regards! |
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-12-28 : 03:54:50
|
For example:SELECT DISTINCT table1.hostFROM table1 LEFT JOIN table2 ON table1.host=table2.hostWHERE table2.host IS NULL;Devart,SQL Server Tools:dbForge Data StudiodbForge Schema ComparedbForge Data ComparedbForge SQL Complete |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-12-28 : 04:09:02
|
try thisselect host from table1 t1 where not exists(select 1 from table2 t2 where t1.host=t2.host) Also are you using SQL server?PBUH |
|
|
arya6000
Starting Member
3 Posts |
Posted - 2010-12-28 : 04:12:26
|
Thanks to both of you, its working now, I'm using PostgreSQL |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-12-28 : 05:14:47
|
This is a SQL Server forum, we're going to assume that questions are related to SQL Server. PostgreSQL has different syntax, different rules and anything we suggest that will work for SQL may not work for that.Suggest you post PostgreSQL questions on a PostgreSQL forum in future.--Gail ShawSQL Server MVP |
|
|
|
|
|