Jump to content
php.lv forumi

MYSQL pieprasijums


dAivEd

Recommended Posts

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.

Link to comment
Share on other sites

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();

userpoints_1262451456.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...