I have a project in ASP.Net (C#) connected to a game database. I have a player
table and I want to get the total value of the playtime
row from all the players with the same account_id
. My last logic was this:
MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
String queryStr;
String connStringPlayer = System.Configuration.ConfigurationManager.ConnectionStrings["PlayerDB"].ToString();
conn = new MySql.Data.MySqlClient.MySqlConnection(connStringPlayer);
conn.Open();
queryStr = "SELECT playtime FROM player.player WHERE account_id='" + AccountId + "' LIMIT 5";
cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
{
Int32 countpt = (Int32)cmd.ExecuteScalar();
String playcount = countpt.ToString();
AccountInfoTotalGameMinuetsValue.Text = playcount;
}
conn.Close();
In this way my AccountInfoTotalGameMinuetsValue
Label displays only the playtime
of the 1st player of this account_id
(2 players exist in this account).
SUM is what you are looking for
SUM([DISTINCT] expr)
Returns the sum of expr. If the return set has no rows, SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of expr.
SUM() returns NULL if there were no matching rows.
So the following query Will give you the totals for all the players
SELECT account_id, SUM(playtime) FROM player.player GROUP BY account_id;
if you want the information for just one player
SELECT account_id, SUM(playtime) FROM player.player
WHERE account_id = some_id
GROUP BY account_id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.