Q.1) Suppose I have a table named advertisements
with a field ad_memberships
containing comma separated 1,2,3,4 type values and I want to calculate the total number of rows for each values then how can I do that? Like say total rows in the table containing value 1 is 10, 2 is 5, 3 is 15, 4 is 3 etc., without manually inputting the data in FIND_IN_SET() function? I just want to automatically calculate for all. Here, 1,2,3,4
is coming from a table named memberships
where these are the unique IDs of different rows mbs_id
.
Q.2) This is related to the first question and both needs to be done together. I am writing it separately so that it is clear and understandable. So, suppose I also have a second table named ads_category
with fields ac_id
and ac_value
. This ac_id
is inserted in 1st table advertisements
in the field ads_category
so that all ads are relevant to some different category. Now after calculating the rows in the 1st question I want to multiply is ac_value
with the number of rows received in first question and display the result as follows.
Standard Membership: 10 ads (Value: $5)
Premium Membership: 16 ads (Value: $12)
Elite Membership: 8 ads (Value: $7)
Ultimate Membership: 11 ads (Value: $8)
and so on....
This means for members with standard memberships 10 ads are available for members holding premium membership 16 ads are available and so on. How can I accomplish that? For an example, the database structure is given below:
table: memberships
mbs_id | mbs_name
1 | Standard
2 | Premium
3 | Elite
4 | Ultimate
table: advertisements
ad_id | ad_memberships | ad_category
1 | 1,2 | 1
2 | 1,2,3 | 1
3 | 1,3 | 2
4 | 2,3 | 3
table: ads_category
ac_id | ac_value
1 | 0.5
2 | 1
3 | 1.5
4 | 2
Expected result from the above database structure
Standard : 3 ads (Value: $1.5)
Premium : 3 ads (Value: $3)
Elite : 3 ads (Value: $4.5)
Ultimate : 0 ads (Value: $0)
Now I want to join all the three tables and get the relevant result. And yes, no values can be entered manually in the query. All should be done with the values in the database. How can I do that? I am using PDO. Please help.