2

I have query with Inner Join.

Query 1:

   select *
     from vehicle_models vmodel
     Inner join ogpo_voilure_model md on md.Name = vmodel.VEHICLE_MODEL

Now, i Need data that not exists in these id. With another word - opposite Inner JOIN.

I tried to make query that I need, but not successfull.

Query 2:

Select top 500 *
 from ogpo_voilure_model md
 Where md.id not in
 (
 select md.id
     from Novelty.dbo.vehicle_models vmodel
     Inner join [ogpo_voilure_model] md on md.Name = vmodel.VEHICLE_MODEL
 )

I find in StakOverflow answer like this(sixth example). But my fields are not NULL.

How I can achieve it?

Community
  • 1
  • 1
Шыназ Алиш
  • 401
  • 2
  • 7
  • 23

2 Answers2

0

The inner join means you want everything is in set table vehicle_models but also got one or more correlated row in ogpo_voilure_model

If you define the oposite to it as everything is in vehicle_models but don't got a correlated in ogpo_voilure_model

All you needs is:

select *
     from [Novelty].[dbo].[vehicle_models] vmodel
     Where vmodel.VEHICLE_MODEL not in
     (
       select md.Name
       from [Novelty].[dbo].[ogpo_voilure_model] md
     )

And following that definition it's right even if results returns zero rows. If it's not the right answer you must first define what the opposite of inner join means to you. For example you maybe want to swap the tables.

jean
  • 4,159
  • 4
  • 31
  • 52
0

What do you mean by "my fields are not NULL"?

The link that you gave in the question has an answer: FULL JOIN.

SELECT *
FROM 
    vehicle_models vmodel
    FULL JOIN ogpo_voilure_model md on md.Name = vmodel.VEHICLE_MODEL
WHERE
    md.Name IS NULL
    OR vmodel.VEHICLE_MODEL IS NULL
;

This will return rows from vehicle_models and ogpo_voilure_model that don't have common Vehicle Model Name.

It would help if you could add few sample rows to the question and your expected result.

Community
  • 1
  • 1
Vladimir Baranov
  • 31,799
  • 5
  • 53
  • 90