1

hope someone can help me with this,

im trying to connect a WHERE argument within an MySQL Statement with an IF operation, but i dont know how to handle it...

SELECT  k.EventId AS Id
    , ws.Name AS Name
    , dauer AS Dauer
    , kat.Name AS Kurztyp
    , doz.Name AS Dozent        
    , DATE_FORMAT(k.Begin, '%Y-%m-%d') AS Begin
    , COUNT(an.EventId) AS Teilnehmer
FROM kalender AS k
        LEFT JOIN workshop AS ws        
            ON (k.WorkshopId = ws.Id)
        LEFT JOIN angemeldet AS an      
            ON (k.EventId = an.EventId)
        LEFT JOIN dozent AS doz         
            ON (k.Dozent = doz.Id)
        LEFT JOIN ws_kategorie as kat   
            ON ((ws.Kategorie = kat.Id) or (ws.Kategorie2 = kat.Id))
        INNER JOIN bezahlvariante AS bezvar
                ON (an.bezahlvariante_idbezahlvariante = bezvar.idbezahlvariante)
    WHERE kat.Name LIKE '%security%'
        AND k.Begin > '2014-04-01'
        AND k.aktiv = '1'

---------------->

        AND bezvar.TN_Anzahl_phys = '1'

<----------------- this should be like: IF(COUNT(an.EventId)>0)
THAN bezvar.TN_Anzahl_phys = '1'
ELSE bezvar.TN_Anzahl_phys IN (1,2)

            GROUP BY k.EventId;

How can i use an IF operation like this in MySQL?

1 Answer 1

2

You can use case-when as

GROUP BY k.EventId
having
case 
  when COUNT(an.EventId)>0 then bezvar.TN_Anzahl_phys = '1'
  else
   bezvar.TN_Anzahl_phys IN (1,2)
END
Sign up to request clarification or add additional context in comments.

11 Comments

thats correct it will show invalid use of group let ne correct it
sadly doesnt work, SQL error Error Code: 1054. Unknown column 'bezvar.TN_Anzahl_phys' in 'having clause' idk why
DDL is way to large, only 8000 allowed but has about 20k? :/
wow just have some sample data dont need all may be 10 15 rows.
ouch but usually this kind of syntax should work, try removing the having clause and try to select the col bezvar.TN_Anzahl_phys in select and see if its working. If yes then it should work in the having clause, I just tested in my local mysql using same pattern of query by joining tables and it works well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.