gpt4 book ai didi

MySQL 查询 WHERE -> IF

转载 作者:行者123 更新时间:2023-11-29 06:37:36 25 4
gpt4 key购买 nike

希望有人能帮我解决这个问题,

我正在尝试将 MySQL 语句中的 WHERE 参数与 IF 操作连接起来,但我不知道如何处理它...

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'

<---------------- 这应该是这样的:IF(COUNT(an.EventId)>0)
比 bezvar.TN_Anzahl_phys = '1'
ELSE bezvar.TN_Anzahl_phys IN (1,2)

            GROUP BY k.EventId;

如何在 MySQL 中使用这样的 IF 操作?

最佳答案

你可以使用case-when作为

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

关于MySQL 查询 WHERE -> IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com