---
title: "Join med fyra tabeller"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/141101-join-med-fyra-tabeller"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "devotion"
published: "2006-01-18T18:16:11.000Z"
updated: "2006-01-18T19:43:50.000Z"
replies: 8
views: 425
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/141101-join-med-fyra-tabeller"
---

# Join med fyra tabeller

## #1 — devotion, 2006-01-18T18:16Z

Hej   :(  

Om jag har följande kod:

```
sSQL = 	"SELECT tblExArticle.eNumber, tblExArticle.articleName, tblExArticle.articleUnit, tblExPrice.articlePrice, tblExCategory.articleCategory, " &_
		"tblExCategory.articleCategoryName, tblExPrice.articleSupplierId, tblExSupplier.SupplierName " &_
		"FROM tblExSupplier INNER JOIN (tblExCategory INNER JOIN (tblExArticle INNER JOIN tblExPrice ON tblExArticle.eNumber = tblExPrice.eNumber) " &_
		"ON tblExCategory.articleCategory = tblExPrice.articleCategory) ON tblExSupplier.SupplierId = tblExPrice.articleSupplierId " &_
		"WHERE tblExPrice.articleSupplierId = " & iSupplierId & ""
```

jag joinar fyra tabeller tblExPrice tblExArticle och tblExCategory tblExSupplier

Vad händer om ett fält är tomt (null) då listas inte något eller?

Hur kan man göra för att lista aven om något värde i någon tabell är tomt?

Mvh
Henrik    :(

Permalänk: https://www.webforum.nu/p/141101

## #2 — Peter S, 2006-01-18T18:21Z

Hej!

Experimentera med att byta INNER mot LEFT. t1 LEFT join t2 betyder att tupler från t1 kommer att tas med även om joinen fallerar.

Permalänk: https://www.webforum.nu/p/1748107

## #3 — devotion, 2006-01-18T18:26Z

ok, ska jag köra Left på alla menar du?

Mvh

Permalänk: https://www.webforum.nu/p/1748112

## #4 — @nders, 2006-01-18T18:28Z

Använd LEFT (dvs en OUTER JOIN) när du vill ha resultat oberoende av om joinen träffar eller inte. Träffar inte joinen får du då NULL i alla fält från den "högra" tabellen i joinen.

Mvh

Permalänk: https://www.webforum.nu/p/1748114

## #5 — devotion, 2006-01-18T18:37Z

:(  
Ändrade till:

```
sSQL = 	"SELECT tblExArticle.eNumber, tblExArticle.articleName, tblExArticle.articleUnit, tblExPrice.articlePrice, tblExCategory.articleCategory, " &_
		"tblExCategory.articleCategoryName, tblExPrice.articleSupplierId, tblExSupplier.SupplierName " &_
		"FROM tblExSupplier LEFT JOIN (tblExCategory LEFT JOIN (tblExArticle LEFT JOIN tblExPrice ON tblExArticle.eNumber = tblExPrice.eNumber) " &_
		"ON tblExCategory.articleCategory = tblExPrice.articleCategory) ON tblExSupplier.SupplierId = tblExPrice.articleSupplierId " &_
		"WHERE tblExPrice.articleSupplierId = " & iSupplierId & ""
```

Fick felmeddelande:

```
Feltyp:
Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
[Microsoft][Drivrutin för ODBC Microsoft Access] Kopplingsuttrycket stöds inte.
/articles/administration/addUppdragNuArticles.asp, line 41
```

Använder access....

Mvh
henrik
  :(

Permalänk: https://www.webforum.nu/p/1748121

## #6 — @nders, 2006-01-18T18:39Z

vore fint om du kunde skriva ut färdig sql-fråga och göra den lite mer läsbar :)

mvh

Permalänk: https://www.webforum.nu/p/1748122

## #7 — devotion, 2006-01-18T18:53Z

okej, ska göra ett försök!

```
sSQL = 	"SELECT tblExArticle.eNumber, tblExArticle.articleName, tblExArticle.articleUnit, tblExPrice.articlePrice, tblExCategory.articleCategory, " &_
		"tblExCategory.articleCategoryName, tblExPrice.articleSupplierId, tblExSupplier.SupplierName " &_
		"FROM tblExSupplier " &_ 
		"LEFT JOIN " &_ 
		"(tblExCategory LEFT JOIN (tblExArticle LEFT JOIN tblExPrice ON tblExArticle.eNumber = tblExPrice.eNumber) " &_
		"ON tblExCategory.articleCategory = tblExPrice.articleCategory) " &_
		"ON tblExSupplier.SupplierId = tblExPrice.articleSupplierId " &_
		"WHERE tblExPrice.articleSupplierId = " & iSupplierId & ""
```

Utskriven:

```
SELECT tblExArticle.eNumber, tblExArticle.articleName, tblExArticle.articleUnit, tblExPrice.articlePrice, tblExCategory.articleCategory, tblExCategory.articleCategoryName, tblExPrice.articleSupplierId, tblExSupplier.SupplierName 
FROM tblExSupplier 
LEFT JOIN 
(tblExCategory LEFT JOIN (tblExArticle LEFT JOIN tblExPrice ON tblExArticle.eNumber = tblExPrice.eNumber) 
ON tblExCategory.articleCategory = tblExPrice.articleCategory) 
ON tblExSupplier.SupplierId = tblExPrice.articleSupplierId 
WHERE tblExPrice.articleSupplierId = 1
```

Mvh
henrik

Permalänk: https://www.webforum.nu/p/1748130

## #8 — Peter S, 2006-01-18T19:05Z

Vad händer med:

```
SELECT tblExArticle.eNumber
     , tblExArticle.articleName
     , tblExArticle.articleUnit
     , tblExPrice.articlePrice
     , tblExCategory.articleCategory
     , tblExCategory.articleCategoryName
     , tblExPrice.articleSupplierId
     , tblExSupplier.SupplierName
  FROM ((tblExSupplier 
 LEFT
  JOIN tblExPrice
    ON tblExSupplier.SupplierId = tblExPrice.articleSupplierId)
 LEFT
  JOIN tblExArticle
    ON tblExArticle.eNumber = tblExPrice.eNumber)
 LEFT 
  JOIN tblExCategory  
    ON tblExCategory.articleCategory = tblExPrice.articleCategory
 WHERE tblExPrice.articleSupplierId = " & iSupplierId & ""
```

?

Permalänk: https://www.webforum.nu/p/1748133

## #9 — devotion, 2006-01-18T19:43Z

Det händer bara fina saker... Funkar finfint! :)  

Tack igen Peter S

Nu är jag helglad igen... (nästan i alla fall... det finn säker massa andra skaer som skall strula....)

Tack!

Mvh
Henrik

Permalänk: https://www.webforum.nu/p/1748154

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/141101-join-med-fyra-tabeller
