Mahlzeit liebe Kollegen :=)
Ich habe derzeit ein kleines Problem mit einer MySQL Abfrage, welches mich mittlerweile zum verzweifeln bringt.
Und zwar möchte ich mir aus einer Datenbanktabelle 2 seperate SUM()s ausrechnen lassen, diese dann voneinander Subtrahieren, und nach den endgültigen höchsten Zahlen sortieren.
Ich hab zwar schon mehrere Methoden versucht, komme aber zu keiner Lösung .. :/
1.) Versuch
SELECT SUM(p.account_points_type) AS plus, SUM(m.account_points_type) AS minus, SUM(plus - minus) AS count, u.user_name, u.user_status
FROM tabelle_users AS u
LEFT JOIN tabelle_accounts AS p ON (u.user_id=p.account_owner AND p.account_points_type='1')
LEFT JOIN tabelle_accounts AS m ON (u.user_id=m.account_owner AND m.account_points_type='0')
ORDER BY count DESC
GROUP BY user_id
2.) Versuch
SELECT SUM(p.account_points_type) AS plus, SUM(m.account_points_type) AS minus, u.user_name, u.user_status
FROM tabelle_users AS u
LEFT JOIN tabelle_accounts AS p ON (u.user_id=p.account_owner AND p.account_points_type='1')
LEFT JOIN tabelle_accounts AS m ON (u.user_id=m.account_owner AND m.account_points_type='0')
ORDER BY (plus - minus) DESC
GROUP BY user_id
Usertabelle
user_id | user_name | user_status
-----------+-----------+-------------
1 | Musterm1 | 0
-----------+-----------+-------------
2 | Musterm2 | 0
Punktetabelle
account_id | account_owner | account_points_count | account_points_type | account_timestamp
------------+---------------+----------------------+---------------------+-------------------
1 | 1 | 100 | 1 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
2 | 1 | 70 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
3 | 1 | 25 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
4 | 2 | 200 | 1 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
5 | 2 | 100 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
6 | 2 | 75 | 0 | unix_timestmap ..
Bin über jede Hilfe oder jeden Tipp dankbar!
Danke, liebe Grüße, David
Ich habe derzeit ein kleines Problem mit einer MySQL Abfrage, welches mich mittlerweile zum verzweifeln bringt.
Und zwar möchte ich mir aus einer Datenbanktabelle 2 seperate SUM()s ausrechnen lassen, diese dann voneinander Subtrahieren, und nach den endgültigen höchsten Zahlen sortieren.
Ich hab zwar schon mehrere Methoden versucht, komme aber zu keiner Lösung .. :/
1.) Versuch
SELECT SUM(p.account_points_type) AS plus, SUM(m.account_points_type) AS minus, SUM(plus - minus) AS count, u.user_name, u.user_status
FROM tabelle_users AS u
LEFT JOIN tabelle_accounts AS p ON (u.user_id=p.account_owner AND p.account_points_type='1')
LEFT JOIN tabelle_accounts AS m ON (u.user_id=m.account_owner AND m.account_points_type='0')
ORDER BY count DESC
GROUP BY user_id
2.) Versuch
SELECT SUM(p.account_points_type) AS plus, SUM(m.account_points_type) AS minus, u.user_name, u.user_status
FROM tabelle_users AS u
LEFT JOIN tabelle_accounts AS p ON (u.user_id=p.account_owner AND p.account_points_type='1')
LEFT JOIN tabelle_accounts AS m ON (u.user_id=m.account_owner AND m.account_points_type='0')
ORDER BY (plus - minus) DESC
GROUP BY user_id
Usertabelle
user_id | user_name | user_status
-----------+-----------+-------------
1 | Musterm1 | 0
-----------+-----------+-------------
2 | Musterm2 | 0
Punktetabelle
account_id | account_owner | account_points_count | account_points_type | account_timestamp
------------+---------------+----------------------+---------------------+-------------------
1 | 1 | 100 | 1 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
2 | 1 | 70 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
3 | 1 | 25 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
4 | 2 | 200 | 1 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
5 | 2 | 100 | 0 | unix_timestmap ..
------------+---------------+----------------------+---------------------+-------------------
6 | 2 | 75 | 0 | unix_timestmap ..
Bin über jede Hilfe oder jeden Tipp dankbar!
Danke, liebe Grüße, David