Toon posts:

[MYSQL] Left - Join probleem*

Pagina: 1
Acties:

Verwijderd

Topicstarter
Situatie:

- table accounts met 118000 rows
- table acount_extra met 20000 rows

Wat wil ik? Alle mensen die geen uitgebreide account hebben:

SELECT accounts.account_ID as ID FROM accounts,account_extra WHERE accounts.account_ID!=account_extra.account_ID

Hier krijg ik een oneindig aantal resultaten dus ik:

SELECT accounts.account_ID as ID FROM accounts,profiel_extra LIMIT 2000000

Nu krijg ik dus 2 miljoen !!! rows terug(terwijl er maar 118000 in staan) dus op een of andere manier werken die tabellen niet goed samen.

Weet iemand hoe dit kan?

MOD: Misschien titel verandering in:
[MYSQL] Left join probleem

  • whoami
  • Registratie: December 2000
  • Nu online
Op zaterdag 01 juni 2002 14:24 schreef xuimper het volgende:

SELECT accounts.account_ID as ID FROM accounts,account_extra WHERE accounts.account_ID!=account_extra.account_ID

Hier krijg ik een oneindig aantal resultaten dus ik:

SELECT accounts.account_ID as ID FROM accounts,profiel_extra LIMIT 2000000

Nu krijg ik dus 2 miljoen !!! rows terug(terwijl er maar 118000 in staan) dus op een of andere manier werken die tabellen niet goed samen.
Die tabellen werken wel samen, maar je moet je link wel goed leggen natuurlijk.
Als je een SELECT doet uit 2 of meer tabellen, dan moet je altijd een WHERE tabel1.id =tabel2.id doen en niet !=. Anders krijg je een cartesiaans product.

Hoe moet je het dan wel doen:
- je kunt het met een subquery doen, maar dat ondersteund MySQL waarschijnlijk (?) nog altijd niet. (Dus, select from tabel1 where id not in (select from tabel2).

https://fgheysels.github.io/


  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op zaterdag 01 juni 2002 14:42 schreef whoami het volgende:
maar dat ondersteund MySQL waarschijnlijk (?) nog altijd niet.
Nee, zeker niet :)
En dat duurt ook nog wel even.

mysql biedt in haar manual wel een korte uitleg hoe je 'not in (select ...)' kan herschrijven naar een join of dat ook doet wat je wilt weet ik niet :)

Verwijderd

Topicstarter
Volgens mij ligt het aan:

SELECT accounts.account_ID as ID FROM accounts,profiel_extra LIMIT 2000000

Nu krijg ik dus 2 miljoen !!! rows terug.

Hoe kan dat nou terwijl er maar 118000 in staan????

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op zaterdag 01 juni 2002 14:50 schreef xuimper het volgende:
Volgens mij ligt het aan:

SELECT accounts.account_ID as ID FROM accounts,profiel_extra LIMIT 2000000

Nu krijg ik dus 2 miljoen !!! rows terug.

Hoe kan dat nou terwijl er maar 118000 in staan????
Lees je es in in de wondere wereld van het carthetische product...
Oftewel je krijgt zelfs 118000 * 20000 records terug als je die LIMIT weghaalt.

[edit]
Waarom moest de titel in een 'left join probleem' worden vervangen?
Jouw queries bevatten geen left join hoor? Zelfs geen, oohhzooo belangerijke, equijoin.

Verwijderd

Topicstarter
heb het:

SELECT * FROM accounts LEFT JOIN account_extra on(account_extra.account_ID = accounts.account_ID) WHERE
account_extra.account_ID!='0'

Ik had al een join geprobeerd maar niet de combinatie (account_extra.account_ID = accounts.account_ID) met account_extra.account_ID!='0'

  • dvdhoek
  • Registratie: Februari 2002
  • Laatst online: 30-08 22:07
Op zaterdag 01 juni 2002 14:58 schreef xuimper het volgende:
heb het:

SELECT * FROM accounts LEFT JOIN account_extra on(account_extra.account_ID = accounts.account_ID) WHERE
account_extra.account_ID!='0'

Ik had al een join geprobeerd maar niet de combinatie (account_extra.account_ID = accounts.account_ID) met account_extra.account_ID!='0'
Werkt dit?? Nu krijg je toch ook de records erbij van de mensen die wel een extra account hebben, waarvan het account_ID ongelijk '0' is :?

Ik denk eerder aan
SELECT * FROM accounts LEFT JOIN account_extra ON(account_extra.account_ID = accounts.account_ID) WHERE
account_extra.account_ID IS NULL

Ik heb nooit met MySQL gewerkt, dus weet niet of deze de IS NULL ondersteunt.

Verwijderd

Uit de MySQL manual ( http://www.mysql.com/doc/J/O/JOIN.html ):
If there is no matching record for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find records in a table that have no counterpart in another table:
code:
1
2
3
mysql> SELECT table1.* FROM table1
    ->    LEFT JOIN table2 ON table1.id=table2.id
    ->    WHERE table2.id IS NULL;

This example finds all rows in table1 with an id value that is not present in table2 (that is, all rows in table1 with no corresponding row in table2). This assumes that table2.id is declared NOT NULL, of course. See section 5.2.6 How MySQL Optimises LEFT JOIN and RIGHT JOIN.
Dit toepassen moet nu kinderspel zijn, lijkt me.

Enjoy :)
Pagina: 1