无法检索每条记录的额外查询值?

时间:2013-08-19 21:57:26

标签: php mysql sql join

我在一张桌子上工作,用户将从数据库中挑选数据。

问题是我无法在我的while条件下放两个语句,因为我从不同的表和行中获取数据。

@CODE

<?php
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

$result = mysql_query("select * FROM users");

$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");

$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");

echo "<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
    <th>Username</th>
    <th>Type</th>                                   
    <th>Email</th>
    <th>Last Login</th>
    <th>Last IP</th>
    <th>space_used</th>
    <th>total_files</th>
    <th>status</th>
</tr>";

while($row = mysql_fetch_array($result))
{
    echo "<tr>";
        echo "<td>" . $row['username'] . "</td>";
        echo "<td>" . $row['level'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['lastlogindate'] . "</td>";
        echo "<td>" . $row['lastloginip'] . "</td>";
        echo "<td>" . $space_used['space_used'] . "</td>";
        echo "<td>" . $total_files['total_files'] . "</td>";
        echo "<td>" . $row['status'] . "</td>";
    echo "</tr>";
}

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

我得到的是用户名,级别,电子邮件,lastlogindate,lastloginip&amp;的所有值。状态。在我的桌子上没有显示的两个是我想要实现的第二个条件:space_used和total_files。

我做错了什么?我不是php的专家,事实上我有一个混合的教程和一个脚本,我想要得到这个结果有点复杂。

由于

4 个答案:

答案 0 :(得分:1)

您希望按userId汇总。

将这两个查询拉到while循环中。使用检索到的用户id进行汇总查询。

while($row = mysql_fetch_array($result))
{
    $space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
    $total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
    ...
 }

组合成一个查询。

$agragateDataResponce = mysql_query("SELECT SUM(fileSize) as size,
                                            COUNT(id) as count 
                                       FROM file 
                                      WHERE file.userId={$row['id']} AND 
                                            file.statusId=1");
$agragateData = mysql_fetch_array($agragateDataResponce);
{p> 访问 $agragateData['size']$agragateData['count'];

然后查找如何将剩余的两个SQL组合成一个连接:)。 SO Question on topic

答案 1 :(得分:1)

您需要重新设计查询,可以使用JOIN将3个查询恢复为单个查询:

  SELECT u.*, 
         SUM(f.fileSize) AS space_used, 
         COUNT(f.id) AS total_files
    FROM users u
    JOIN file f 
      ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id

然后你可以这样读:

<?php
// your database info
$db_host = 'your MySQL server host';
$db_user = 'your username';
$db_pass = 'your password';
$db_name = 'your database name';
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($con->connect_error)
{
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

if (!$result = $con->query("SELECT u.*,
                                   SUM(f.fileSize) AS space_used, 
                                   COUNT(f.id) AS total_files
                              FROM users u
                              JOIN file f 
                                ON f.userId = u.id AND f.statusId = 1
                          GROUP BY u.id"))
{
    die('Select query error: ' . $con->error);
}
?>
<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
    <tr>
        <th>Username</th>
        <th>Type</th>                                   
        <th>Email</th>
        <th>Last Login</th>
        <th>Last IP</th>
        <th>space_used</th>
        <th>total_files</th>
        <th>status</th>
    </tr>
<?php
while ($row = $result->fetch_array())
{
?>
    <tr>
        <td><?php echo $row['username']; ?></td>
        <td><?php echo $row['level']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['lastlogindate']; ?></td>
        <td><?php echo $row['lastloginip']; ?></td>
        <td><?php echo $row['space_used']; ?></td>
        <td><?php echo $row['total_files']; ?></td>
        <td><?php echo $row['status']; ?></td>
    </tr>
<?php
}
$con->close();
?>
</table>

答案 2 :(得分:0)

首先查询数据库并将结果保存到变量中。

$result = mysql_query("select * FROM users");
$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");

然后使用

迭代第一个结果
while($row = mysql_fetch_array($result))

请注意您对mysql_fetch_array的来电。你永远不会在其他两个结果上调用mysql_fetch_array

有关详细信息,请参阅PHP documentation on mysql_query。具体来说,返回类型是资源

  

返回的结果资源应该传递给mysql_fetch_array(),以及其他用于处理结果表的函数,以访问返回的数据。

然而,

SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1

甚至不是你想要的。您检查file.userId=users.id但只读取file表。您缺少用户来源表。

答案 3 :(得分:0)

尝试使用

$space_used = mysql_query("SELECT SUM(fileSize) 
                             FROM file 
                            WHERE file.userId=users.id AND 
                                  file.statusId=1");

$total_files = mysql_query("SELECT COUNT(id) 
                              FROM file 
                             WHERE file.userId=users.id AND 
                                   file.statusId=1");

While内的上述行,并使用user.id

进行验证

我是stackoverflow的新手,所以不要我的。我不知道如何使代码看起来像每个人