dAivEd Posted January 1, 2010 Report Share Posted January 1, 2010 SELECT name, COUNT(points) FROM `users` GROUP BY name Datubazes struktura izskatas shita: daived - 3 daived - 4 daived - 1 maris - 3 maris - 3 utt.. Mans pieprasijums izvada CIK niku ir tabula Piemers: daived - 3 ( jo 3 niki tabula ) maris - 2 ( jo 2 niki tabula ) Bet man vajag CIK kopa viniem punktu katram, lai sanaktu shita: daived - 8 maris - 6 Palidziet salikt ludzu. Quote Link to comment Share on other sites More sharing options...
anonīms Posted January 1, 2010 Report Share Posted January 1, 2010 sum(points) where name = ? Quote Link to comment Share on other sites More sharing options...
bubu Posted January 1, 2010 Report Share Posted January 1, 2010 SELECT name, SUM(points) FROM users GROUP BY name Quote Link to comment Share on other sites More sharing options...
anonīms Posted January 1, 2010 Report Share Posted January 1, 2010 a pag, tev vajag tikai, lai izvadās 1 vārds (ja ir vairāki vienādi)? Nezinu kā tur ar bubu kodu, bet tad, ja nemaldos, ir jāizmanto vēl distinct Quote Link to comment Share on other sites More sharing options...
Kemito Posted January 1, 2010 Report Share Posted January 1, 2010 Viņam ir nepieciešams izvadīt skaitu cik šādi niki ir datubāzē! Quote Link to comment Share on other sites More sharing options...
bubu Posted January 1, 2010 Report Share Posted January 1, 2010 Jūs cilvēki jocīgi esat? Topika autors skaidri un gaiši uzrakstīja, ko viņam vajag: Bet man vajag CIK kopa viniem punktu katram, lai sanaktu shita: daived - 8 maris - 6 Nekādu distinct tur nevajag. Quote Link to comment Share on other sites More sharing options...
2easy Posted January 2, 2010 Report Share Posted January 2, 2010 iešu palīgā bubu, lai būtu 2:2 :D ja nemaldos, ir jāizmanto vēl distinct nope, pietiek ar GROUP BY Viņam ir nepieciešams izvadīt skaitu cik šādi niki ir datubāzē! ej labāk izgulies test... function qs($s) { // sagatavo stringu for mysql query return is_null($s) ? 'NULL' : "'" . mysql_real_escape_string($s) . "'"; } function go($sSql) { // mysql query ar error reportingu $h = mysql_query($sSql) or die('<b>mysql error ' . mysql_errno() . ':</b> ' . mysql_error() . '<br /><b>query:</b> ' . substr($sSql, 0, 1000)); return $h; } function userPointsLoad() { // izveido/ielādē testa datus go('DROP TABLE IF EXISTS UserPoints'); go('CREATE TABLE UserPoints (User varchar(20) NOT NULL, Points int NOT NULL) ENGINE=MyISAM'); go('INSERT UserPoints (User, Points) VALUES ' . '(' . qs('daived') . ', 3), ' . '(' . qs('daived') . ', 4), ' . '(' . qs('daived') . ', 1), ' . '(' . qs('maris') . ', 3), ' . '(' . qs('maris') . ', 3)' ); } function userPointsList($h) { // izdrukā useru punktu sarakstu no dotā recordset echo '<table cellpadding="1" cellspacing="0" border="1" style="float: left; margin-right: 20px"><tr><th>User</th><th>Points</th></tr>'; while ($r = mysql_fetch_object($h)) echo '<tr><td>' . $r->User . '</td><td>' . $r->Points . '</td></tr>'; echo '</table>'; } function userPointsSelTest() { // pārbauda SELECTus ar/bez DISTINCT userPointsList(go('SELECT DISTINCT User, Points FROM UserPoints')); // nav tas, kas vajadzīgs //userPointsList(go('SELECT User, SUM(Points) AS Points FROM UserPoints')); // mysql error 1140: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause userPointsList(go('SELECT User, SUM(Points) AS Points FROM UserPoints GROUP BY User')); // bingo ;) userPointsList(go('SELECT DISTINCT User, SUM(Points) AS Points FROM UserPoints GROUP BY User')); // same (taču šeit no DISTINCT nav nekāda ieguvuma) } userPointsLoad(); userPointsSelTest(); Quote Link to comment Share on other sites More sharing options...
marrtins Posted January 2, 2010 Report Share Posted January 2, 2010 Grūti saprast, kas te ir vajadzīgs, bet, ja pareizi sapratu, tad tā: SELECT name, SUM(skaits) summa FROM (SELECT name, COUNT(points) skaits FROM `users` GROUP BY name) AS temp GROUP BY name Quote Link to comment Share on other sites More sharing options...
bubu Posted January 3, 2010 Report Share Posted January 3, 2010 marrtins: kas par šausmām? Kāpēc jūs visi (izņemot 2easy) neizlasat autora jautājumu, kurā ir konkrēts piemērs ar datiem no kā un ko viņam vajag dabūt. Quote Link to comment Share on other sites More sharing options...
marrtins Posted January 3, 2010 Report Share Posted January 3, 2010 Kāpēc šausmas? Pārprasts jautājums, tas arī viss. 5x izlasot, sapratu (laikam), ko viņam tur vajag. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.