首頁  >  問答  >  主體

在MySQL中使用2個表進行計算

我有 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粉821231319P粉821231319189 天前360

全部回覆(2)我來回復

  • P粉295616170

    P粉2956161702024-03-31 09:37:42

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

    範例查詢

    回覆
    0
  • P粉488464731

    P粉4884647312024-03-31 00:53:56

    雷雷

    回覆
    0
  • 取消回覆