简体   繁体   English

使用mySQL PHP数据库进行数学运算

[英]Maths with mySQL PHP database

I'm looking into doing some maths to work out the total amount of 'credits' all users in a database have. 我正在考虑做一些数学运算,以计算出数据库中所有用户拥有的“积分”总数。

I have a database with a table called users with userid, username, email and credit columns. 我有一个数据库,其中有一个名为users,userid,username,email和credit列的表。 I was wondering if there is a way of diplaying all the results in a table like userid, username, email and credits as table colums and then at the bottom have the total of all user credits: 我想知道是否有一种方法可以将表中的所有结果(例如userid,用户名,电子邮件和信用)显示为表列,然后在底部显示所有用户信用的总数:

 UserID | Username | Email Address | Credits
 -------+----------+---------------+--------
    1   | example  | e@mail1.com   |    4 
    2   | another  | e@mail2.com   |    3
    3   | lastone  | e@mail3.com   |    1
 -------+----------+---------------+--------
        |          | Total Credits |    8
 -------+----------+---------------+--------

I was wondering if this was possible and any other math equations you can do with mySQL databases and PHP 我想知道这是否可行以及您可以使用mySQL数据库和PHP进行的任何其他数学方程式

Oliver 奥立佛

If you need to do this all in one query, you can use WITH ROLLUP in certain conditions. 如果您需要在一个查询中完成所有操作,则可以在某些条件下使用WITH ROLLUP

http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

In particular, read the comments at the bottom of that link for examples related to what you are trying to do. 特别是,请阅读该链接底部的注释,以获取与您要执行的操作有关的示例。

Personally, I prefer to do aggregates separately, or on the application side. 就我个人而言,我更喜欢单独或在应用程序方面进行聚合。

You can use SUM(Credits) as Total Credits . 您可以使用SUM(Credits) as Total Credits More info here 更多信息在这里

You could do : 你可以做:

select UserId, Username, EmailAddress, Credits from users order by userid
union
select '' as UserId,'' as Username,'Total Credits' as email,sum(credits) as credits from users;

First of all all you need to do is just create the database in MySQL, if you don't know how to create here's the syntax all you need to do just run the query but don't forget to create the database first. 首先,您需要做的就是在MySQL中创建数据库,如果您不知道如何创建此处的语法,则只需运行查询即可,但不要忘记首先创建数据库。

create table mhs ( UserID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, example varchar(20), Email_Address varchar(30), Credits INT ) 创建表mhs(UserID INT非空AUTO_INCREMENT PRIMARY KEY,例如varchar(20),Email_Address varchar(30),Credits INT)

After you execute the database now we just need to configure it with the php file. 在执行数据库之后,现在我们只需要使用php文件进行配置。

Here's the list of code in php you can choose anyway to write your own code 这是php中的代码列表,您仍然可以选择编写自己的代码

<?php

showdatame();
function showdatame()
{
$inttotalcredits=0;
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("praktikum", $con);

$result = mysql_query("SELECT * FROM mhs");

echo "<table border='1'>
<tr>
<th>UserId</th>
<th>UserName</th>
<th>Email Adress</th>
<th>Credits</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
$inttotalcredits += $row[3];
echo "</tr>";
}
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "Total Credits" . "</td>";
echo "<td>" . $inttotalcredits . "</td>";

echo "</table>";
mysql_close($con);   
}

?>

I hope this quite help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM