Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
SQL Select statement help
Name: mking (by mkingrey) Date: April 12, 2006 at 14:23:02 Pacific OS: xp CPU/Ram: n/a Product: n/a
Comment:
I have Table1 with FieldA and Table2 with FieldA. I would like to query Table1 for all records that DO NOT exist in FieldA of Table2. I have tried the following but it doesn't filter correctly. Any suggestions?? Thx!!
SELECT Table1.FieldA FROM Table1, Table2 WHERE Table1.FieldA <> Table2.Field
Name: Michael J (by mjdamato) Date: April 12, 2006 at 15:24:11 Pacific
Reply:
I'm not real good with SQL, but I think the solution would be to join the two tables (inner or outer?) such that every record in tableA would have it's corresponding value from B as well. Then put in your where clause that you only want records where the B value is null.
This probably isn't right, but it should be the right direction:
SELECT Table1.FieldA as field1, Table2.FieldA as field2 FROM Table1, Table2 WHERE Table1.FieldA=Table2.FieldA AND field2 IS NULL
Michael J
0
Response Number 2
Name: mdow Date: April 13, 2006 at 06:27:32 Pacific
Reply:
SELECT TABLE1.ID, TABLE2.ID FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.ID WHERE (((TABLE2.ID) Is Null));
0
Response Number 3
Name: babaji Date: April 15, 2006 at 10:41:54 Pacific
Reply:
Try this
SELECT Table1.FieldA FROM Table1 WHERE Table1.FieldA NOT in ( select Table2.FieldA from Table2 )
Both mdow and babaji have correct solutions (And Michael, except the join should be a left join as mdow suggested) , but mdow's will be much, much faster. You should avoid having subqueries in the WHERE statement whenever possible.
Good luck, -SN
0
Response Number 5
Name: Michael J (by mjdamato) Date: April 16, 2006 at 13:15:11 Pacific
Reply:
As I said, I didn't think my structure was quite right, but I was pretty sure of the logic behind it.
Summary: Hello!! i need some help in building a sql server select statement: i have a table with these columns and info: product_id | quantity ------------------------ prod1 | 2 prod1 | ...
Summary: Hi all, I need a help in MS SQL Express query. I have a table named "tblTotalCust" and the fields are: [Date] date, [ServedTime] date, [TotalCustomer] int. Example data: [Date] ...
Summary: Hi, You can limit the number of rows in the output of a query by using the rowcount parameter as sql > select * from emp where rowcount < 2; hope it will help u :-) ...