Skip to content
Advertisement

Select all from one table where some columns match another select

I have two differents tables. The have some columns in common, for this example lets say ‘name’ and ‘id’.

By making

I get a list of the elements that are on one tablet but not in the other one.

Up to this point everything is OK.

But now, I want to make a select all from table1 where the name and the id matches the result of the query above.

Advertisement

Answer

After lots of comments I think this is what you’re after…

You may allow implicit casting to work but above is the explicit approach.

If not then you would need to cast the string dates to a date time, assuming Event_Time_UTC is a date/time datatype.

A left join lets us return all records from the 1st table and only those that match from the 2nd.

The t1.* returns only the columns from table1. The join criteria (on) allows us to identify those records which match so they can then be eliminated in the where clause by ‘where t2.name is null’ they will always be null when no record match in t2.

Thus you get a result set that is: all records from t1 without a matching record on name and id in table2.

enter image description here

Old version

The below content is no longer relevant, based on comments.

I redacted previous answer a lot because you’re using SQL Server not MySQL and I know you want multiple records not table1 and table2 joined.

In the below I create two tables: table1 and table2. I then populate table1 and table2 with some sample data

I then show how to get only those records which exist in one table but not the other; returning a separate ROW for each. I then go into detail as to why I choose this approach vs others. I’ll finally review what you’ve tried and try to explain why I don’t think it will work.

Option 1

which results in…

enter image description here

Notice John isn’t there as it’s in both tables. We have the other 2 records from table1, and the ID, name from table2 you’re after.

Normally I would do this as a full outer join but since I think you want to reuse the name and id fields to relate to BOTH tables in the same column we had to use this approach and spell out all the column names in table 1 and put NULL for each column in table1 when displaying records from table2 in order to make the output of the second query union to the first. We simply can’t use *

Option 2: Using a full outer join… with all columns from T1

you get this… which doesn’t show Ringo…

enter image description here

But then I would ask why you need anything from Table 2 at all so I think you’re wanting to still show the ID, Name from table2 when it doesn’t exist in table1.

Which is why What I think you’re after is the results from the 1st query using the union all.

Option 3 I suppose we could avoid the second query in option 1 by doing…

which gives us what I believe to be the desired results as well.

This works because we know we only want the name,id from table2 and all the column values in table1 will be blank.

Notice however in all cases we simply can’t use Tablename.* to select all records from table1.

enter image description here

This is what you tried:

  1. Assuming you want to reuse the ID, Name fields; you can’t select *. Why? because the records in Table2 not in table1 aren’t in table1. In my example if you want Ringo to show up you have to reference table2! Additionally, * gives you no ability to “Coalesce” the ID and name fields together as I did in option 3 above.
  2. If you ONLY want the columns from table1, that means you will NEVER see data from table2. If you don’t need the data from table2, (such as ringo in my example) then why do we need to do the union at all?) I’m assuming you want ringo, thus you HAVE to somewhere reference name, id from table2.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement