lundi 27 juillet 2015

MYSQL if field value add part in query

I have some problem with my Mysql query. I'd like to have some IF or case based on a field value, if the field value is equal to 1 it should add a part in the query. In my case: if 'GB.uitgaand RICHT' is equal to 1 it should add something like 'AND DIENSTID = :dienstID'

To tell more about the query, it's a query that gets all the letters out of the database, in case it's a letter that's received it is seen by all the departments, if its a letter that has been send it should only be visible by the department that have send the letter.

SELECT GB.id IDEN, GB.briefdatum DATUM, 
     GA.naam AFZEN, GT.naam TREF, GB.aangetekend AANG, 
     GB.uitgaand RICHT, 
    (SELECT SGD.naam 
     FROM geg_dienst SGD, geg_doorgave SGDG 
     WHERE SGDG.dienst_id = SGD.id AND SGDG.brief_id = GB.id 
     ORDER BY SGDG.date DESC 
     LIMIT 1) DIENST, 
    (SELECT SGD.id FROM geg_dienst SGD, geg_doorgave SGDG 
     WHERE SGDG.dienst_id = SGD.id AND SGDG.brief_id = GB.id 
     ORDER BY SGDG.date DESC 
     LIMIT 1) DIENSTID 
FROM geg_brieven GB, geg_afzender GA, geg_trefwoord GT 
WHERE GB.afzender = GA.id AND GB.trefwoord = GT.id

Does anyone have any clue how to do it? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire