Question

AGENT DB TABLE

agent_id  agent_name company_name
--------  ----------  -----------
1         AAA           XXX
2         BBB           YYY
3         CCC           ZZZ
4         DDD           XYZ

DRIVER DB TABLE

agent_id  driver_id   driver_name
--------  ----------  -----------
2         1           EEE
2         2           FFF
2         3           GGG
1         4           HHH
3         5           III
3         6           JJJ

SUBSCRIPTION DB TABLE

agent_id  subscription_id   pricing_plan
--------  --------------    -----------
1         1                 3
2         2                 1
3         3                 0
4         4                 2

I have three kind of pricing_plans in my subscription table. Here, I want to control the agent to add new drivers in his account. For example, agent_id 2 (BBB) have pricing_plan (1). if pricing_plan is 1 that specific agent can add 5 drivers only in his account. Then he could not add no more drivers in his account. (after 5th driver)

Another example, agent_id 4 (DDD) have pricing_plan (2). if pricing_plan is 2 that specific agent can add 25 drivers only in his account. Then he could not add no more drivers in his account. (after 25th driver) I want to count the number of drivers depending upon loggedin agent_id and pricing_plan and do some condition between pricing_plan and number of drivers.

I HAVE TRIED THIS BELOW QUERY

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 ) 

THIS QUERY RETURNS THIS OUTPUT

 no_of_drivers    agent_id       pricing_plan
 ------------- --------------    -----------
    1                1                 3
    3                2                 1
    2                3                 0
    0                4                 2 

I can get the number of drivers of the agent_id and I have the pricing_plan option too. But, I don't know how to do the condition between these two columns no_of_drivers & pricing_plan and depending upon the number of drivers & pricing_plan of agent_id they cannot add more drivers.

pricing plan 1 => add 5 drivers only

pricing plan 2 => add 25 drivers only

pricing plan 3 => add unlimited drivers

Was it helpful?

Solution

This can be pretty trivially done using a case statement. I've put together an SQLFiddle that I think answers the question you're ultimately trying to ask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top