[SQL] select 2 tabellen, ook lege kolommen zien

Pagina: 1
Acties:
  • 102 views sinds 30-01-2008
  • Reageer

  • Makkie80
  • Registratie: Juni 2001
  • Laatst online: 01-09 19:44

Makkie80

Makkie voor Makkie!

Topicstarter
Weet iemand hoe ik ook rijen kan weergeven waarbij er in onderstaand voorbeeld geen afdelingshoofd is ingevuld voor een afdeling. Dus ALLE afdelingen weergeven ongeacht of er een afdelingshoofd is toegewezen of niet. (zo niet, dan blijft b.medewerkernaam leeg. Ik heb geprobeerd te zoeken, maar weet niet goed hoe ik hierop moet zoeken, dus kan iemand me helpen?
code:
1
2
3
SELECT a.afdeling_id, a.afdelingnaam, b.medewerkernaam
FROM afdeling a, medewerker b
WHERE a.afdelingshoofd = b.medewerker_id;

Ik gebruik trouwens MYSQL.

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
mischien toevoegen
or a.afdelingshoofd is null

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”


  • Varienaja
  • Registratie: Februari 2001
  • Laatst online: 14-06-2025

Varienaja

Wie dit leest is gek.

Je moet eens even zoeken op left of right outer joins.

* Varienaja kan zo snel geen voorbeeldje vinden.

Siditamentis astuentis pactum.


  • Makkie80
  • Registratie: Juni 2001
  • Laatst online: 01-09 19:44

Makkie80

Makkie voor Makkie!

Topicstarter
Op maandag 01 juli 2002 12:05 schreef rwb het volgende:
mischien toevoegen
or a.afdelingshoofd is null
Dit werkt niet... ik meende dat je ergens iets omheen kon zetten ofzo, iets met EXISTS() ofzo... :?

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

Op maandag 01 juli 2002 12:14 schreef Makkie80 het volgende:

[..]

Dit werkt niet... ik meende dat je ergens iets omheen kon zetten ofzo, iets met EXISTS() ofzo... :?
www.mysql.com/doc
ga es ff op zoek dan </hint>

Doet iets met Cloud (MS/IBM)


  • Makkie80
  • Registratie: Juni 2001
  • Laatst online: 01-09 19:44

Makkie80

Makkie voor Makkie!

Topicstarter
Op maandag 01 juli 2002 12:15 schreef D2k het volgende:

[..]

www.mysql.com/doc
ga es ff op zoek dan </hint>
Ja.. heb ik al gedaan.. kon tot dusver nog niks vinden.. hoopte dat iemand het hier zo ff wist.. Weet ook niet goed waarop ik moet zoeken namelijk...

  • Varienaja
  • Registratie: Februari 2001
  • Laatst online: 14-06-2025

Varienaja

Wie dit leest is gek.

Op maandag 01 juli 2002 12:18 schreef Makkie80 het volgende:
Weet ook niet goed waarop ik moet zoeken namelijk...
Welwaar: left of right outer joins.

Siditamentis astuentis pactum.


  • Makkie80
  • Registratie: Juni 2001
  • Laatst online: 01-09 19:44

Makkie80

Makkie voor Makkie!

Topicstarter
Op maandag 01 juli 2002 12:19 schreef Varienaja het volgende:

[..]

Welwaar: left of right outer joins.
*Gevonden... probeert het nog te snappen...*

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

En dan is een left join het best, omdat die het meest ondersteund worden.
Er is geen goede reden te bedenken waarom je dan perse toch een right join moet gebruiken :)

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
ik heb even voor je gekeken in de sql server docs

Using Left Outer Joins
Consider a join of the authors table and the publishers table on their city columns. The results show only the authors who live in cities in which a publisher is located (in this case, Abraham Bennet and Cheryl Carson).

To include all authors in the results, regardless of whether a publisher is located in the same city, use an SQL-92 left outer join. The following is the query and results of the Transact-SQL left outer join:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors a LEFT OUTER JOIN publishers p
ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

Here is the result set:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
au_fname         au_lname                pub_name       
-------------------- ------------------------------ ----------------- 
Reginald         Blotchet-Halls          NULL
Michel         DeFrance              NULL
Innes           del Castillo             NULL
Ann         Dull                   NULL
Marjorie         Green                NULL
Morningstar     Greene               NULL
Burt             Gringlesby            NULL
Sheryl         Hunter                NULL
Livia           Karsen               NULL
Charlene         Locksley                NULL
Stearns       MacFeather               NULL
Heather       McBadden               NULL
Michael       O'Leary               NULL
Sylvia         Panteley              NULL
Albert         Ringer                NULL
Anne             Ringer              NULL
Meander       Smith               NULL
Dean             Straight                NULL
Dirk             Stringer                NULL
Johnson       White               NULL
Akiko           Yokomoto                 NULL
Abraham       Bennet                 Algodata Infosystems
Cheryl         Carson                Algodata Infosystems

(23 row(s) affected)

The LEFT OUTER JOIN includes all rows in the authors table in the results, whether or not there is a match on the city column in the publishers table. Notice that in the results there is no matching data for most of the authors listed; therefore, these rows contain null values in the pub_name column.

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”


  • Makkie80
  • Registratie: Juni 2001
  • Laatst online: 01-09 19:44

Makkie80

Makkie voor Makkie!

Topicstarter
Bedankt! Het is gelukt... M'n query is nu:
code:
1
2
3
4
5
SELECT a.afdeling_id, a.afdelingsnaam, m.medewerker_id, 
     m.voornaam, m.achternaam, m.tussenvoegsel, m.afdeling
FROM afdeling a left join medewerker m
ON a.afdelingshoofd = m.medewerker_id
ORDER BY a.afdelingsnaam
Pagina: 1