4
CREATE TABLE IF NOT EXISTS tablename (u_id INT NOT NULL, a_id MEDIUMINT NOT NULL,s_count MEDIUMINT NOT NULL, weighted FLOAT NOT NULL)ENGINE=INNODB; INSERT INTO tablename (u_id,a_id,s_count,weighted) VALUES (1,1,17,0.0521472392638),(1,2,80,0.245398773006),(1,3,2,0.00613496932515),(1,4,1,0.00306748466258),(1,5,1,0.00306748466258),(1,6,20,0.0613496932515),(1,7,3,0.00920245398773),(1,8,100,0.306748466258),(1,9,100,0.306748466258),(1,10,2,0.00613496932515),(2,1,1,0.00327868852459),(2,2,1,0.00327868852459),(2,3,100,0.327868852459),(2,4,200,0.655737704918),(2,5,1,0.00327868852459),(2,6,1,0.00327868852459),(2,7,0,0.0),(2,8,0,0.0),(2,9,0,0.0),(2,10,1,0.00327868852459),(3,1,15,0.172413793103),(3,2,40,0.459770114943),(3,3,0,0.0),(3,4,0,0.0),(3,5,0,0.0),(3,6,10,0.114942528736),(3,7,1,0.0114942528736),(3,8,20,0.229885057471),(3,9,0,0.0),(3,10,1,0.0114942528736);
私が何をしたいの簡易版で再作成することができ
+---------+-----------+------------+------------+ | u_id | a_id | count | weighted | +---------+-----------+------------+------------+ | 1 | 1 | 17 | 0.0521472 | | 1 | 2 | 80 | 0.245399 | | 1 | 3 | 2 | 0.00613497 | | 1 | 4 | 1 | 0.00306748 | | 1 | 5 | 1 | 0.00306748 | | 1 | 6 | 20 | 0.0613497 | | 1 | 7 | 3 | 0.00920245 | | 1 | 8 | 100 | 0.306748 | | 1 | 9 | 100 | 0.306748 | | 1 | 10 | 2 | 0.00613497 | | 2 | 1 | 1 | 0.00327869 | | 2 | 2 | 1 | 0.00327869 | | 2 | 3 | 100 | 0.327869 | | 2 | 4 | 200 | 0.655738 | | 2 | 5 | 1 | 0.00327869 | | 2 | 6 | 1 | 0.00327869 | | 2 | 7 | 0 | 0 | | 2 | 8 | 0 | 0 | | 2 | 9 | 0 | 0 | | 2 | 10 | 1 | 0.00327869 | | 3 | 1 | 15 | 0.172414 | | 3 | 2 | 40 | 0.45977 | | 3 | 3 | 0 | 0 | | 3 | 4 | 0 | 0 | | 3 | 5 | 0 | 0 | | 3 | 6 | 10 | 0.114943 | | 3 | 7 | 1 | 0.0114943 | | 3 | 8 | 20 | 0.229885 | | 3 | 9 | 0 | 0 | | 3 | 10 | 1 | 0.0114943 | +---------+-----------+------------+------------+
よう
SELECT u_id, SUM(weighted) as total FROM tablename WHERE a_id IN (1,2,3,4,5,6,7,8,9) GROUP BY u_id ORDER BY total DESC;で見えるデータのテーブルを持っています
結果が得られる
+---------+-------------------+ | u_id | total | +---------+-------------------+ | 2 | 0.996721301227808 | | 1 | 0.993865059688687 | | 3 | 0.988505747169256 | +---------+-------------------+
私がやりたい、より複雑なバージョンがそう
query 1 SELECT count FROM tablename WHERE u_id = 1
からの結果を取ることは次のようになり
+-----------+------------+ | a_id | count | +-----------+------------+ | 1 | 17 | | 2 | 80 | | 3 | 2 | | 4 | 1 | | 5 | 1 | | 6 | 20 | | 7 | 3 | | 8 | 100 | | 9 | 100 | | 10 | 2 | +-----------+------------+
を返す、U_IDからのカウントに基づいて結果を重み付けすることです合計を計算するために使用される、 woと計算する私は単一のクエリでこれを行うことができますどのように
sum(count value from query 1 * weighting value for u_id = 3 for each a_id)
17 * 0.172413793 =2.931034483 80 * 0.459770115 =36.7816092 2 * 0 =0 1 * 0 =0 1 * 0 =0 20 * 0.114942529 =2.298850575 3 * 0.011494253 =0.034482759 100 * 0.229885057 =22.98850575 100 * 0 =0 2 * 0.011494253 =0.022988506 sums up to 65.05747126
ことによって行うことがULD?
こんにちはベン、私は合計を計算したい|カウントu_id = 1 *重み付けu_id = 1 |カウントu_id = 1 *重み付けu_id = 2 |カウントu_id = 1 *重み付けu_id = 3 – Ben
私はあなたのソリューションは、カウントu_id = 1 *重み付けu_id = 1 |カウントu_id = 2 *重み付けu_id = 2 |カウントu_id = 3 *重み付けu_id = 3? – Ben
@Ben、あなたの質問を誤解しました。今私は、私の答えを更新して、それを行う方法を示しました。 –