Berechnung mit 2 Tabellen in MySQL
P粉821231319
P粉821231319 2024-03-30 11:32:48
0
2
358

Ich habe 2 Tische, Spiele und Handel Ich verwende diese Formel sum(EntryFee * Rake/(100 + Rake)*TotalEntry) in der Spieletabelle, um den Wert zu erhalten

Ich verwende diese Abfrage in der Anzahl der Transaktionstabellen (verschiedene Benutzer-IDs), um die Werte zu erhalten

Jetzt möchte ich den Wert von [sum(EntryFee * Rake/(100 + Rake)*TotalEntry)] durch den Wert von [count(distinct UserID)] dividieren

Zum Beispiel sum(EntryFee * Rake/(100 + Rake)*TotalEntry) = 90 und count(eindeutige Benutzer-ID) = 3 Dann ist 90/3 =30 Wie kann ich das in MYSQL machen

P粉821231319
P粉821231319

Antworte allen(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
SELECT (
    SELECT sum(EntryFee * Rake/(100 + Rake)*TotalEntry) FROM Games
)/(
    SELECT count(distinct UserID) FROM Transaction
) MyResult
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!