代理数据库表
agent_id agent_name company_name
-------- ---------- -----------
1 AAA XXX
2 BBB YYY
3 CCC ZZZ
4 DDD XYZ驱动数据库表
agent_id driver_id driver_name
-------- ---------- -----------
2 1 EEE
2 2 FFF
2 3 GGG
1 4 HHH
3 5 III
3 6 JJJ订阅数据库表
agent_id subscription_id pricing_plan
-------- -------------- -----------
1 1 3
2 2 1
3 3 0
4 4 2我的订阅表里有三种pricing_plans。在这里,我想控制代理在他的帐户中添加新的驱动程序。例如,agent_id 2 (BBB)有pricing_plan (1)。如果pricing_plan是1,该特定代理只能在他的帐户中添加5个驱动程序。然后,他不能再在他的帐户中添加司机了。(在第5名司机之后)
另一个例子是agent_id 4 (DDD)有pricing_plan (2)。如果pricing_plan是2,那么特定的代理只能在他的帐户中添加25个驱动程序。然后,他不能再在他的帐户中添加司机了。(在第25个驱动程序之后),我想根据loggedin、agent_id和pricing_plan来计算驱动程序的数量,并在pricing_plan和驱动程序的数量之间做一些条件。
我已经尝试过以下查询
SELECT COUNT( * ) `no_of_drivers` , s . *
FROM ta_drivers d, ta_subscription s
WHERE d.agent_id = s.agent_id
AND s.pricing_plan = (
SELECT pricing_plan
FROM ta_subscription
WHERE agent_id = $agent_id ) 此查询返回此输出。
no_of_drivers agent_id pricing_plan
------------- -------------- -----------
1 1 3
3 2 1
2 3 0
0 4 2 我可以得到agent_id的驱动程序的数量,我也有pricing_plan选项。但是,我不知道如何在这两列no_of_drivers & pricing_plan之间设置条件,并且取决于驱动程序的数量和agent_id的pricing_plan,它们不能添加更多的驱动程序。
定价方案1 =>只添加5个驱动程序
定价方案2 =>只添加25个驱动程序
定价方案3 =>添加无限驱动程序
发布于 2014-04-25 05:49:30
使用案例语句可以很轻松地做到这一点。我把SQLFiddle放在一起,我认为它回答了你最终想要问的问题。
https://stackoverflow.com/questions/23284803
复制相似问题