I have two tables, table a has two columns , email and course. table b has one column email. I am trying to get a result from table a that will exclude all the similar emails in table b.I used this
SELECT * FROM EnqAll
LEFT OUTER JOIN Unsubscribe
ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS null
it returned the result with three columns email, course and email(this last column is null). I tried to create a table with this,but I couldn’t, on further reading I was advised to first create the table in the database and insert, but I cant create a table with similar column name, please how can I go about this, I am using sql-server
because i was using * oit was creating a third column with value for email. so i could not create a table to insert the result(because the result had three column , email, course and email)
If EnqAll has only two fields, and in your query only view those two fields, then it shouldn’t be a problem. Even if you use *
The way I understand it, the table EnqAll only has two fields: email and course.
SELECT * FROM EnqAll will only show two fields because EnqAll has only two fields.
The third field (unsubscribe.email) will not be in the result because it is only a criteria.
Now, if you wanted the three fields visible, you only need to specificy the table
SELECT EnqAll.* , Unsubscribe.* FROM EnqAll LEFT JOIN Unsuscribe ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS null
That should work too.
Although, the better way to do it would be naming the fields
SELECT EnqAll. email, EnqAll.course , Unsubscribe.email FROM EnqAll LEFT JOIN Unsuscribe ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS null
Again, this should work too. To make it prettier to look at, you can use aliases for the fields
SELECT EnqAll. email as E-mail, EnqAll.course as Course , Unsubscribe.email as Unsubscribed FROM EnqAll LEFT JOIN Unsuscribe ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS null
[quote=“molona, post:6, topic:101513, full:true”]Although, the better way to do it would be naming the fields
SELECT EnqAll. email, EnqAll.course , Unsubscribe.email FROM EnqAll LEFT JOIN Unsuscribe ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS null[/quote]how would this be the "better" way?
the third column will still have a duplicated column name and it will always be NULL
But he’s joining two tables so the SELECT * returns the columns from both tables, not only the two from the first table. I gave the solution in my first reply
Better way if he wanted to see the three fields on screen (even if it was just to check that third one was, as desired, null)
I only added it because in his first post he said that the result showed 3 fields, the last one being null. I don’t understand how that can happen when he was selecting the fields of EnqAll which was supposed to have only two fields
That I expect an SQL query to behave as it should (* means all columns, not just a few because of some obscure logic) but I know that Access does things it’s own way. So if SQL server has that same characteristic it’s no wonder I didn’t understand what you were trying to say in your posts
I obviously didn’t explain myself very well… * does select all the fields of the table used in the FROM. But the second table is only used to add the criteria. And that’s why he needs the join. The second table is not part of the FROM
the original query had the unadorned “select star” –SELECT * FROM EnqAll
LEFT OUTER JOIN Unsubscribe
ON EnqAll.EMAIL = Unsubscribe.Email
WHERE Unsubscribe.EMAIL IS nullthis means return all columns from all tables in the FROM clause and it works the same way in all databases
come on, folks, the second table ~is~ part of the FROM clause