如何向查询添加其他WHERE子句。有这个疑问:
SELECT round(Sum(Zarada), 2) AS Zarada,
cms_cart_customers.Firstname,
cms_cart_wallet.WalletType as WW ,
cms_cart_customers.Lastname,
FROM_UNIXTIME(`cms_cart_customers`.`d_ins`, " %d.%m.%Y") AS Datum_formatiran
FROM (cms_cart_customers)
LEFT JOIN cms_cart_wallet
ON cms_cart_wallet.IdBuyer =cms_cart_customers.Id
WHERE `cms_cart_customers`.`IDReferral` = '199' AND
if(ww = 7) THEN ' cms_cart_wallet.WalletType =7' END IF
GROUP BY cms_cart_customers.Id
如果选择cms_cart_wallet.WalletType为Ww则添加新的附加子句WHERE cms_cart_wallet.WalletType为7。有可能以这种方式做到吗???
答案 0 :(得分:0)
您可以通过在左连接中添加一个子句来实现此目的,条件如下所示。
SELECT round(Sum(Zarada), 2) AS Zarada, cms_cart_customers.Firstname,cms_cart_wallet.WalletType as WW ,cms_cart_customers.Lastname, FROM_UNIXTIME(cms_cart_customers.d_ins, " %d.%m.%Y") AS Datum_formatiran FROM (cms_cart_customers) LEFT JOIN cms_cart_wallet ON cms_cart_wallet.IdBuyer =cms_cart_customers.Id and cms_cart_wallet.WalletType =7 WHERE cms_cart_customers.IDReferral = '199' GROUP BY cms_cart_customers.Id
答案 1 :(得分:0)
您可以直接在where子句中添加它而不使用任何别名。
SELECT round(Sum(Zarada),2)AS Zarada, cms_cart_customers.Firstname,cms_cart_wallet.WalletType为WW, cms_cart_customers.Lastname,FROM_UNIXTIME(cms_cart_customers.d_ins,“%d。%m。%Y”)AS Datum_formatiran FROM(cms_cart_customers) LEFT JOIN cms_cart_wallet ON cms_cart_wallet.IdBuyer = cms_cart_customers.Id WHERE cms_cart_customers.IDReferral ='199'和cms_cart_wallet.WalletType = 7 GROUP BY cms_cart_customers.Id
答案 2 :(得分:0)
您不需要if
。简单的条件逻辑就足够了:
WHERE cms_cart_customers.IDReferral = '199' AND
(ww <> 7 or cms_cart_wallet.WalletType = 7)
处理NULL
值:
WHERE cms_cart_customers.IDReferral = '199' AND
(COALESCE(ww, 0) <> 7 or cms_cart_wallet.WalletType = 7)