从另一个表SQL加入

时间:2017-09-10 19:36:12

标签: php mysql

我需要使用以下代码将用户数据包加入到IP帐户

SELECT ip_address, SUM(upload_bytes) as upload_bytes, SUM(download_bytes) as 
download_bytes 
FROM ((SELECT src_address as ip_address, SUM(bytes) AS upload_bytes, 0 as 
download_bytes
FROM ipaccounting
WHERE src_address BETWEEN ('192.168.0.1') AND ('192.168.255.254') AND 
dst_address NOT BETWEEN ('192.168.0.1') AND ('192.168.255.254') GROUP BY 
src_address)
UNION ALL (SELECT dst_address, 0 AS upload_bytes, SUM(bytes) as 
download_bytes 
FROM ipaccounting WHERE dst_address BETWEEN ('192.168.0.1') AND 
('192.168.255.254') AND src_address NOT BETWEEN ('192.168.0.1') AND 
('192.168.255.254') GROUP BY dst_address))
a GROUP BY ip_address 

我的SQL数据库有3个表:ipaccounting,users和datapackages。 每个数据包都有一个唯一的ID,ID在users表的datapackage列中指定,IP地址在users表中指定。

我需要加入用户数据包装信息和IP地址,以便回复:

IP地址|上传字节|下载字节|总字节数| Datapackage ID |用户名

我的整个代码:

<?php
//Include needed files
require ("config.php");
include ("includes/formatbytes.php");

//Connect to database
$conn = mysqli_connect($SQLserver, $SQLusername, $SQLpassword, 
$SQLdatabase);
if (!$conn) {
    die("Could not connect: " . mysqli_connect_error());
}


//Convert IP Addresses
$IPRangeStart = ip2long($IPRangeStart);
$IPRangeEnd = ip2long($IPRangeEnd);

//Query database
$query = "
    SELECT ip_address, SUM(upload_bytes) as upload_bytes, 
SUM(download_bytes) as download_bytes 
    FROM ((SELECT src_address as ip_address, SUM(bytes) AS upload_bytes, 0 
as download_bytes
    FROM ipaccounting
    WHERE src_address BETWEEN INET_NTOA($IPRangeStart) AND 
INET_NTOA($IPRangeEnd) AND dst_address NOT BETWEEN INET_NTOA($IPRangeStart) 
AND 
INET_NTOA($IPRangeEnd) GROUP BY src_address)
    UNION ALL (SELECT dst_address, 0 AS upload_bytes, SUM(bytes) as 
download_bytes FROM ipaccounting WHERE dst_address BETWEEN 
INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) AND src_address NOT 
BETWEEN 
INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) GROUP BY dst_address))
    a GROUP BY ip_address ORDER BY INET_ATON(ip_address)";


//Execute query
$result = mysqli_query($conn,$query);


//Display IP information
echo "<table>";
echo "<tr><th>IP Address</th><th>Upload</th><th>Download</th><th>Total</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
        $ip_address = $row['ip_address'];
        $upload_bytes = $row['upload_bytes'];
        $download_bytes = $row['download_bytes'];
        $total_bytes = ($upload_bytes + $download_bytes);

        $total_bytes = formatBytes($total_bytes);
        $upload_bytes = formatBytes($upload_bytes);
        $download_bytes = formatBytes($download_bytes);




    echo "<tr><td>".$ip_address."</td><td>".$upload_bytes."</td>
<td>".$download_bytes."</td><td>".$total_bytes."</td></tr>";
} 

echo "</table>";
mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:1)

也许会这样做:

SELECT ip_address, SUM(upload_bytes) as upload_bytes, SUM(download_bytes) as download_bytes, 
sum(upload_bytes + download_bytes) as totalbytes, datapackage_id, username 
FROM 
(
   (SELECT ipaccounting.src_address as ip_address, SUM(ipaccounting.bytes) AS upload_bytes, 0 as download_bytes, 
    user.username, datapackages.datapackage_id
    FROM ipaccounting
    inner join user on user.ipaddress = ipaccounting.src_address
    inner join datapackages on datapackages.datapackage_id = user.datapackageid
    WHERE src_address BETWEEN INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) 
    AND dst_address NOT BETWEEN INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) 
    GROUP BY src_address)
UNION ALL 
   (SELECT ipaccounting.dst_address as ip_address, 0 AS upload_bytes, SUM(ipaccounting.bytes) as download_bytes, 
    user.username, datapackages.datapackage_id
    FROM ipaccounting 
    inner join user on user.ipaddress = ipaccounting.dst_address
    inner join datapackages on datapackages.datapackage_id = user.datapackageid
    WHERE dst_address BETWEEN INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) 
    AND src_address NOT BETWEEN INET_NTOA($IPRangeStart) AND INET_NTOA($IPRangeEnd) 
    GROUP BY dst_address)
) a 
GROUP BY ip_address 
ORDER BY INET_ATON(ip_address)

您可能需要调整一些列名。