2017-10-10 66 views
-2

我怎樣才能獲得前3名的記錄與最高得分之和價格總和加起來一定值(前25或小於25)。這是我的表我試圖找出MySQL查詢

CREATE TABLE test_1id INT(11)NOT NULL, Spiller VARCHAR(45)DEFAULT NULL, Score雙DEFAULT NULL, Pris雙DEFAULT NULL, PRIMARY KEY(id) )ENGINE = InnoDB的默認字符集= UTF8;

INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(1,'a1',7.87,11.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(2,'a2',8.32,10.4); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(3,'a3',6.32,11.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(4,'a4',4.15,8.5); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(5,'a5',4.5,8.5); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(6,'a6',11.13,12.5); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(7,'a7',8.12,10.4); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(8,'a8',5.21,7.5); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(9,'a9',3.77,7); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(10,'a10',4.38,5.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(11,'a11',2.69,5.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(12,'a12',4.48,7.4); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(13,'a13',3.47,5.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(14,'a14',4.87,6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(15,'a15',4.6,6.6); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(16,'a16',2.74,6。3); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(17,'a17',5.54,10.4); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(18,'a18',3.86,7.2); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(19,'a19',6.4,5); INSERT INTO testtest_1id,Spiller,Score,Pris)VALUES(20,'a20',2.94,5.9);

,我想等預先this.Thanks :)

Image here

+0

你的問題不真的有道理。你想獲得前三名的得分記錄嗎?或者三條記錄合計爲25? –

+0

三條記錄總計爲25 – Priya

+2

將樣本表格數據添加爲格式化文本,而不是圖像。還有預期的結果。 – jarlh

回答

0
SELECT r1.Spiller, r2.Spiller, r3.Spiller 
FROM MyTable as r1, MyTable as r2, MyTable as r3 
WHERE r1.Pris+r2.Pris+r3.Pris = 25 
+0

它會返回重複在spiller案例記錄與pris = [5,15] –

+0

@VladBochenin是的:它會返回很多東西 –

+0

是的,你是正確的,但我想要結果25或小於25.如果結果不等於25,那麼它會給出接下來的3個記錄少於25個。不過,我忘了解釋一下。 – Priya

0

下面的代碼是結果選擇3行,其具有最高 「的PRI」

SELECT * FROM tableName ORDER BY Pris DESC LIMIT 3; 

編輯:我理解錯誤的問題,但選擇最高的「Pris」編3行,這可能是你的答案,但這不是你的問題的答案

+0

是的,當然不是。我想從表中得到3條記錄,其總和等於25或小於25. – Priya

2

我想是這樣的

select r1.spiller, r2.spiller, r3.spiller 
from pris r1 
join pris r2 on r1.spiller > r2.spiller 
join pris r3 on r2.spiller > r3.spiller 
where r1.pris + r2.pris + r3.pris = 25; 

它可以變成一個頂級的3

select r1.spiller, r2.spiller, r3.spiller, r1.pris+r2.pris+r3.pris 
from pris r1 
join pris r2 on r1.spiller > r2.spiller -- self-join, use > on the primary key to avoid duplicates 
join pris r3 on r2.spiller > r3.spiller 
where r1.pris + r2.pris + r3.pris <= 25 
order by r1.pris+r2.pris+r3.pris desc 
limit 3; 

設置:

CREATE TABLE pris (
    spiller varchar(3) NOT NULL, 
    pris decimal(4,1) DEFAULT NULL 
); 


insert into 
pris(spiller, pris) 
values 
('a1', 11.6), ('a2', 10.4), ('a3', 11.6), 
('a4', 8.5), ('a5', 8.5), ('a6', 12.5), 
('a7', 8.4), ('a8', 7.5),('a9', 7), 
('a10', 7.6), ('a11', 5.6), ('a12', 7.4), 
('a13', 5.6), ('a14', 6), ('a15', 6.6), 
('a16', 6.3), ('a17', 10.4), ('a18', 7.2), 
('a19', 5), ('a20', 5.9); 

結果:

+---------+---------+---------+------+ 
| spiller | spiller | spiller | sum | 
+---------+---------+---------+------+ 
| a8  | a6  | a19  | 25.0 | 
| a8  | a20  | a1  | 25.0 | 
| a18  | a17  | a12  | 25.0 | 
+---------+---------+---------+------+ 
+0

它返回很多行,但我只需要3條記錄 – Priya

+0

@Priya我認爲這是主要的重點,你應該真的自己弄清楚細節 – Friso

+0

謝謝,我們能否讓其他3位助手(球員)明智地與其他隊友一起回合並接下來的隊列 – Priya