[MySQL] left join en count met uitkomst 0

Pagina: 1
Acties:

  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Ik gebuik in een query een left join. Uit de tweede tabel tel ik met COUNT een aantal regels.

Als hij geen bijpassende regel kan vinden in starhalo_units en COUNT dus eigenijk op 0 zou moeten komen, geeft hij heel de regel niet weer. Is er een mogelijkheid, dat hij de regel wel weergeeft en bij COUNT de waarde 0 terug geeft, inplaats van geen resultaat?
code:
1
2
3
4
5
6
7
8
9
10
SELECT 
  starhalo_unittypes.unittype_id,
  COUNT(starhalo_units.unit_id) 
FROM starhalo_unittypes 
LEFT JOIN starhalo_units 
  USING(unittype_id) 
WHERE 
  starhalo_units.player_id='1' 
GROUP BY 
  starhalo_unittypes.unittype_id

  • Dikdoan
  • Registratie: November 2000
  • Laatst online: 05-06-2025

Dikdoan

Kijk, ik kan nog lopen

oeps zit niet op te letten.

if the else statement. en gebruik dan een putstatement.

Zolang ie het blijft doen....


  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Hmm ja, ik kan wel vrij veel in sql, maar IF werk ik eigenlijk nooit mee. Ik kan zelf wel ff zoeken, maar het is natuurlijk makkelijker als iemand gewoon het antwoord geeft :P

En ik vraag me af of dat kan, want hij geeft heel de regel niet weer, dus je kunt niet kijken of er 0 uit komt.

  • Dikdoan
  • Registratie: November 2000
  • Laatst online: 05-06-2025

Dikdoan

Kijk, ik kan nog lopen

SELECT
starhalo_unittypes.unittype_id,
COUNT(starhalo_units.unit_id)
FROM starhalo_unittypes
LEFT JOIN starhalo_units
USING(unittype_id)
WHERE starhalo_units.player_id='1'
AND IF count = ' ' Then count = '0'
GROUP BY
starhalo_unittypes.unittype_id

ik denk dat het zoiets moet zijn..

Zolang ie het blijft doen....


  • CyberSnooP
  • Registratie: Augustus 2000
  • Laatst online: 31-03 16:47

CyberSnooP

^^^^ schrijft --->

Voor zover ik weet zit er echt geen IF in de SQL-implementatie van MySQL.

Probeer
code:
1
2
3
4
SELECT 
  starhalo_unittypes.unittype_id,
  COUNT(starhalo_units.unit_id) + 0
FROM ...

|_____vakje______|


  • Crazy D
  • Registratie: Augustus 2000
  • Laatst online: 17:39

Crazy D

I think we should take a look.

Je join omdraaien (right join dus of de FROM en LEFT JOIN tabellen omwisselen).
(maar ik kan het mis hebben want mijn hoofd wil maar niet wakker worden :z Dat werkte iig bij mij altijd wel, en in mssql gooi ik dan vaak een isNull() er omheen zodat als dat record niet gevonden kon worden, ik geen NULL terug krijg maar een default value (0 of '') maar dat hoeft met een Count volgens mij niet, die geeft dan toch wel 0 terug als ie niks vind)

Exact expert nodig?


  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

CyberSnooP:
Voor zover ik weet zit er echt geen IF in de SQL-implementatie van MySQL.
code:
1
2
3
4
5
6
7
mysql> SELECT IF(1 > 0,'jaja','neenee') as de_waarheid;
+-------------+
| de_waarheid |
+-------------+
| jaja    |
+-------------+
1 row in set (0.01 sec)

;)

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Op maandag 25 maart 2002 12:39 schreef Crazy_D het volgende:
Je join omdraaien (right join dus of de FROM en LEFT JOIN tabellen omwisselen).
(maar ik kan het mis hebben want mijn hoofd wil maar niet wakker worden :z Dat werkte iig bij mij altijd wel, en in mssql gooi ik dan vaak een isNull() er omheen zodat als dat record niet gevonden kon worden, ik geen NULL terug krijg maar een default value (0 of '') maar dat hoeft met een Count volgens mij niet, die geeft dan toch wel 0 terug als ie niks vind)
Dit werkt niet. Ik heb er al voor gezorgd dat hij hem wel weer geeft als hij in de ene tabel niet bestaat, maar het gaat eigenlijk om die WHERE.

Als ik het goed begrijp is hier dus geen oplossing voor :(

  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Op maandag 25 maart 2002 12:25 schreef Dikdoan het volgende:
SELECT
starhalo_unittypes.unittype_id,
COUNT(starhalo_units.unit_id)
FROM starhalo_unittypes
LEFT JOIN starhalo_units
USING(unittype_id)
WHERE starhalo_units.player_id='1'
AND IF count = ' ' Then count = '0'
GROUP BY
starhalo_unittypes.unittype_id

ik denk dat het zoiets moet zijn..
Hmm, misschien heb ik het niet helemaal goed uitgelegd.

Maar het is ook wel verekte moeilijk om uit te leggen ;)

Het probleem ligt hem bij "starhalo_units.player_id='1'" Doordat het een LEFT JOIN is hoeft er in starhalo_units geen bijpassend record te zijn. Als die er niet is, laat hij hem wel zien, en vult hij bij player_id gewoon NULL in. Maar ik controleer dus op player_id. Aangezien er NULL staat laat hij heel de regel dus niet zien.
Die starhalo_units.player_id, moet alleen effect hebben op COUNT(starhalo_units.unit_id) en niet op de hele regel.

DUS als ik dit zou doen
code:
1
2
3
4
5
6
7
8
SELECT 
  starhalo_unittypes.unittype_id,
  COUNT(starhalo_units.unit_id) 
FROM starhalo_unittypes 
LEFT JOIN starhalo_units 
  USING(unittype_id) 
GROUP BY 
  starhalo_unittypes.unittype_id

zou er helemaal niets aan de hand zijn, en zou hij netjes 0 weergeven bij de count. Maar ik wil dus het aantal weten waar starhalo_units.player_id='1'.

Het zou dus iets moeten zijn als dit:
code:
1
2
3
4
5
6
7
8
9
10
SELECT 
  starhalo_unittypes.unittype_id,
  COUNT(starhalo_units.unit_id) 
FROM starhalo_unittypes 
LEFT JOIN starhalo_units 
  USING(unittype_id) 
WHERE 
  IFF starhalo_units.player_id<>NULL THEN starhalo_units.player_id='1' 
GROUP BY 
  starhalo_unittypes.unittype_id

Nu weet ik niet helemaal hoe IF werkt, dus ik doe maar wat en ik heb gezocht op IF en IFF op mysql.com, maar kan daar niets over vinden. Ik vraag me dus af of het wel kan in mysql.

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

Als je dit
drm:
code:
1
2
3
4
5
6
7
mysql> SELECT IF(1 > 0,'jaja','neenee') as de_waarheid;
+-------------+
| de_waarheid |
+-------------+
| jaja    |
+-------------+
1 row in set (0.01 sec)

;)
niet overtuigend genoeg vindt, dan moet je het zelf maar even op je mysql prompt proberen.

kortom, IF wordt wel ondersteund door MySQL

afgezien van het feit of je dat hier gaat helpen, maar goed...

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Op maandag 25 maart 2002 14:41 schreef drm het volgende:
Als je dit
[..]

niet overtuigend genoeg vindt, dan moet je het zelf maar even op je mysql prompt proberen.

kortom, IF wordt wel ondersteund door MySQL

afgezien van het feit of je dat hier gaat helpen, maar goed...
Hehe, ik had moeten zoeken op IF() klopt idd, zit wel IF in :)

  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Het moet dus zo iets zijn als dit
code:
1
2
3
4
5
6
7
8
9
10
11
SELECT 
  starhalo_unittypes.unittype_id,
  COUNT(starhalo_units.unit_id) 
FROM starhalo_unittypes 
LEFT JOIN starhalo_units 
  USING(unittype_id) 
WHERE 
  starhalo_units.player_id='1'
  OR ísNULL(starhalo_units.player_id)
GROUP BY 
  starhalo_unittypes.unittype_id

Ja zo iets is het. Heel dicht in de buurt. Precies zo eigenlijk... DIT IS HET! :P

  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Te vroeg gejuicht, dit is het toch niet :( werkt alleen als de starhalo_units tabel helemaal leeg is.

Verwijderd

Misschien gewoon een union maken met
starhalo_unittypes met count en een
starhalo_unittypes zonder count maar met een harde 0 in het resultaat.

  • Fles
  • Registratie: Augustus 2001
  • Laatst online: 06-04-2023
Als ik hem begrijp, werkt hij niet, want dan is starhalo_units.player_id nog NULL

  • eborn
  • Registratie: April 2000
  • Laatst online: 12-04 21:32
UNION zit in MySQL alleen in versie 4.0.0 (of hoger). En ik denk het hier nog om een 3.x.x versie gaat?!
Pagina: 1