Home > Mobile >  Calculation using 2 tables in mysql
Calculation using 2 tables in mysql

Time:06-08

I have 2 tables Games & Transaction I use this formula in Games table, sum(EntryFee * Rake/(100 Rake)*TotalEntry) to get a value

I use this query in Transaction table count(distinct UserID) to get a value

Now i want to divide the value of [sum(EntryFee * Rake/(100 Rake)*TotalEntry)] and value of [count(distinct UserID)]

for eg sum(EntryFee * Rake/(100 Rake)*TotalEntry) = 90 and count(distinct UserID) = 3 then 90/3 =30 How can i do this in MYSQL

CodePudding user response:

SELECT (SELECT sum(EntryFee * Rake/(100 Rake)*TotalEntry) FROM Games)/ (SELECT count(distinct UserID) FROM Transaction) MyResult

CodePudding user response:

CREATE TABLE Games (EntryFee INT, Rake INT, TotalEntry INT);
CREATE TABLE Transaction1 (UserID VARCHAR(25));

INSERT INTO Games VALUES 
    (30,16,150),(45,20,100),(15,5,50),(25,20,300),(10,8,270);

INSERT INTO Transaction1 VALUES ('Daniel'),('David'),('John'),('Martha');

SELECT Games.EntryFee, Games.Rake, Games.TotalEntry, COUNT(distinct Transaction1.UserID) AS CountUser,
(Games.EntryFee * Games.Rake / (100   Games.Rake) * Games.TotalEntry / COUNT(distinct Transaction1.UserID))
AS Calculate
FROM Games JOIN Transaction1 GROUP BY Games.EntryFee, Games.Rake, Games.TotalEntry;

Result :

 ========== ====== ============ =========== ============== 
| EntryFee | Rake | TotalEntry | CountUser | Calculate    |
 ========== ====== ============ =========== ============== 
| 10       | 8    | 270        | 4         | 50.00000000  |
 ---------- ------ ------------ ----------- -------------- 
| 15       | 5    | 50         | 4         | 8.92857500   |
 ---------- ------ ------------ ----------- -------------- 
| 25       | 20   | 300        | 4         | 312.50000000 |
 ---------- ------ ------------ ----------- -------------- 
| 30       | 16   | 150        | 4         | 155.17242500 |
 ---------- ------ ------------ ----------- -------------- 
| 45       | 20   | 100        | 4         | 187.50000000 |
 ---------- ------ ------------ ----------- -------------- 

sample query

  • Related