2016-04-27 13 views
1

playertrackerテーブルでは、プレイヤーID(PID)が等しい値が最も高い行に関連付けられたデッキ名が表示されます。与えられた値に。代わりに、最初のデッキ名が最大額とともに返されます。サンプルコードの最大値と異なる値を返すMySQL

create table players(
    pid int(10), 
    pname varchar(20), 
    favcard varChar(20), 
    mstplayed varChar(20), 
    lestplayed varChar(20) 
); 

create table playertracker(
    pid int(10), 
    deckName varchar(20), 
    amount int(10) 
); 

Insert into players 
    values(1, 'joe', 'swim', 'jump', 'fall'),(2, 'jane', 'up', 'jump', 'fall'),(3, 'jack', 'up', 'jump', 'fall'), 
    (4, 'joe', 'up', 'all', '5'),(5, 'joe', 'up', 'red', 'fall'); 

Insert into playertracker 
    values('1','jump','2'),('1','up','4'),('1','swim','9'),('1','fall','9'), 
    ('2','jump','8'),('2','up','4'),('2','swim','1'),('2','fall','1'), 
    ('3','jump','1'),('3','up','8'),('3','swim','9'),('3','fall','4'), 
    ('4','jump','9'),('4','up','8'),('4','swim','1'),('4','fall','1'), 
    ('5','jump','1'),('5','up','4'),('5','swim','4'),('5','fall','8'), 
    ('6','jump','4'),('6','up','9'),('6','swim','1'),('6','fall','1'); 


    select deckname, max(amount) from playertracker where pid = 1; 

Link to the offending SQL

+0

リンクが壊れているようです... – Aconcagua

+0

ねえ、まだ動作していると思います。私はそこでそれを試しました – bloopiebloopie

+0

@bloopiebloopieリンクは壊れていません。その作業 –

答えて

0

これはクエリであるが

swim 9 
fall 9 

よう

ので、2行

select deckname, amount from playertracker where pid = 1 
and amount = (select max(amount) from playertracker where pid = 1) ; 

を返します

select deckname, amount from playertracker where pid = 1 
and amount = (select max(amount) from playertracker where pid = 1) 
limit 1 ; 
+0

制限1、あなたの笑い。私はなぜ他の声明がうまくいかないのか分からない。 – bloopiebloopie

+0

私はちょうど2つの結果のあなたのクエリ – scaisEdge

+0

あなたの警告を選択したのでcolumn1、max(column2)はfirts colum1の値を返すので、行の値ではありません。 。 – scaisEdge

0
select deckname, max(amount) from playertracker where pid = 1 group by deckname; 
+0

こんにちは、このユーザーに属するすべてのデッキを返します。私は "orderby"と "limit 1"を使って最大値と最小値を得ることができました。もっとエレガントな方法が必要だと思っただけです。 – bloopiebloopie

+0

問題がどのように解決されたのか説明した方が、あなたの応答がより役に立ちます。 –

関連する問題