我有2个MySQL表
“ MerchantAccount” 它具有以下列:
然后我有另一个表作为跟踪器,每月使用了多少。
“商家处理”
它包含以下列:
- ID
-日期
-中(provider
来自“ MerchantAccount”
我想要的是找到一个随机的PROVIDER,它没有使用每月的最大值,例如在“ maxPerMonth”中,例如1500个事务,那么提供者必须达到最大值。它必须按顺序打印一个随机提供程序,以便将所有事务正确分配到表中的所有提供程序上(因此使用了used + 1)。在“商家处理”表中找到在相关提供者上进行每月交易的方法。已经尝试了,但是不起作用
我尝试http://www.sqlfiddle.com/#!9/03b168/26
SELECT a.provider, a.inactive, b.mid, b.date, a.used,
Count(b.id) AS usedThisMonth
FROM merchantaccount a,
merchantprocessing b
WHERE a.inactive = '0'
AND ( a.provider = b.mid
AND Year(b.date) = Year(CURRENT_DATE())
AND Month(b.date) = Month(CURRENT_DATE())
)
AND a.used < usedThisMonth
ORDER BY a.used ASC
LIMIT 1 `
“ where子句”中的未知列“ usedThisMonth”
答案 0 :(得分:0)
像这样尝试:
select IdRisorseUmane,IdUtenteInserimento,u1.Nome+' '+u1.Cognome as InseritoDA,ExtraPreventivo,
u2.Nome+' '+u2.Cognome as Risorsa,
RisorseUmane.IdUtente,IdCantiere,
CONVERT(VARCHAR(10), Data, 105) as Data,
Descrizione,
convert(varchar(5), OreInizio, 108) as OreInizio,
convert(varchar(5), OreFine, 108) as OreFine,
REPLACE(Pausa, '.', ':') as Pausa,
convert(varchar(5),Cast(convert(varchar(5), (OreFine - OreInizio), 108) as datetime) - CAST(REPLACE(Pausa, '.', ':') as datetime), 108) as TotaleOre,
TipologiaPreventivo.NomeTipologia as Tipologia
from RisorseUmane
inner join Utente as u1 on u1.IdUtente = RisorseUmane.IdUtenteInserimento
inner join Utente as u2 on u2.IdUtente = RisorseUmane.IdUtente
inner join TipologiaPreventivo on TipologiaPreventivo.IdTipologiaPreventivo=RisorseUmane.IdTipologia
请注意,这不会产生任何行,因为它们都不满足SELECT a.provider, a.inactive, b.mid, b.date, a.used,
Count(b.id) AS usedThisMonth
FROM merchantaccount a,
merchantprocessing b
WHERE a.inactive = '0'
AND ( a.provider = b.mid
AND Year(b.date) = Year(CURRENT_DATE())
AND Month(b.date) = Month(CURRENT_DATE())
)
GROUP BY a.provider, a.inactive, b.mid, b.date, a.used
HAVING a.used < Count(b.id)
LIMIT 1
子句。
答案 1 :(得分:0)
这使您可以比较每月使用的金额和每个商人的最大金额:
select a.provider, a.inactive,
year(p.date), month(p.date),
count(p.id) as used_this_month,
a.maxPerMonth
from merchantaccount a join
merchantprocessing p
on a.provider = p.mid
where a.inactive = 0 -- no quotes, because this is a number
group by a.provider, a.inactive, a.maxPerMonth,
year(p.date), month(p.date);
您可以添加having a.maxPerMonth >= count(b.id)
以获得超出最大值的数字。
您可以添加p.date >= curdate() - interval (1 - day(curdate()) day
以仅获取当月的数据。