0

I have a select SQL query which is really big and it should be pulling in about 5000 records. But when I use the JOIN It cuts the number of records to say 1000 because it only shows records where a value exists on the joined value, how would I go about pulling all records no matter whether the Join finds that a value exists or NOT?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
karlstackoverflow
  • 3,298
  • 6
  • 30
  • 41
  • You want to use an outer join. There's a good answer here: http://stackoverflow.com/questions/38549/sql-difference-between-inner-and-outer-join – bucko Feb 22 '12 at 00:35
  • It could be that you need a [semi-join](http://stackoverflow.com/a/7036894/15354). There are a number of ways of implementing this; if you do go with outer join then add the DISTINCT keyword to the SELECT clause (assuming it is not already present) otherwise you may experience the number of rows in the result **increasing**. – onedaywhen Feb 22 '12 at 11:40

2 Answers2

2

Left outer join : MSDN Outer Joins

Eric H
  • 1,759
  • 1
  • 11
  • 14
  • This was what I tried, only realised it was correct because I thought it was giving other values in the other table but it was just my hidden records. I knew it was a noob question!. I'm rewarding answer to you because you look like first in – karlstackoverflow Feb 22 '12 at 00:44
  • Don't forget to add the `DISTINCT` keyword to the `SELECT` clause (assuming it is not already present) otherwise you may experience the number of rows in the result **increasing**! – onedaywhen Feb 22 '12 at 11:34
1

Instead of performing an inner join, perform a left outer join

dustyhoppe
  • 1,783
  • 16
  • 20