在MySQL中使用2個表進行計算
P粉821231319
P粉821231319 2024-03-30 11:32:48
0
2
444

我有 2 張桌子 遊戲和交易 我在 Games 表中使用此公式 sum(EntryFee * Rake/(100 Rake)*TotalEntry) 來取得值

我在事務表計數(不同的使用者ID)中使用此查詢來取得值

現在我想將 [sum(EntryFee * Rake/(100 Rake)*TotalEntry)] 的值除以 [count(distinct UserID)] 的值

例如 sum(EntryFee * Rake/(100 Rake)*TotalEntry) = 90 且 count(distinct UserID) = 3 那麼 90/3 =30 我怎麼能在 MYSQL 中做到這一點

P粉821231319
P粉821231319

全部回覆(2)
P粉295616170
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;

結果:

+==========+======+============+===========+==============+
| 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 |
+----------+------+------------+-----------+--------------+

範例查詢

P粉488464731

雷雷

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板