mysql - Is there a way to select a certain number of row for a certain column? -


मेरे पास यह तालिका है:

  + ---- + ----- ---- + ---------- + ------- + | आईडी | User_id | अन्य_आईडी | स्कोर | + ---- + --------- + ---------- + ------- + | 1 | 1 | 1 | 80 | | 2 | 1 | 2 | 40 | | 3 | 1 | 3 | 60 | | 4 | 2 | 2 | 70 | | 5 | 2 | 4 | 50 | | 6 | 2 | 3 | 40 | | 7 | 2 | 5 | 90 | + ---- + --------- + ---------- + ------- +   

मैं सॉर्ट करना चाहता हूं प्रत्येक user_id द्वारा स्कोर और प्रत्येक user_id के लिए अधिकतम n (जैसे, 2) पंक्तियां लौटाएं। इसलिए मैं परिणाम को प्राप्त करना चाहता हूं:

  + --- --- --- --- + ---- + | 1 | 1 | 1 | 80 | | 3 | 1 | 3 | 60 | | 7 | 2 | 5 | 90 | | 4 | 2 | 2 | 70 | + --- --- --- ----- ---- +    

<पी > इसे आसानी से एक सबसेल का उपयोग करके, एक ही तालिका के साथ गिनती की तुलना करके किया जा सकता है
  SELECT * FROM Table1 t (SELECT COUNT (*) से Table1 t1 WHERE t1.user_id = t.user_id और t1.score & gt; = t.score) & lt; = 2 ORDER BY t.user_id एएससी, टीसीओसीकोर DESC   



Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -