gpt4 book ai didi

php - 从另一个表 SQL 连接

转载 作者:行者123 更新时间:2023-11-29 10:37:34 25 4
gpt4 key购买 nike

我需要使用以下代码将用户数据包加入到 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

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

我需要加入用户数据包信息和IP地址,这样它就会回显:

IP 地址 |上传字节 |下载字节 |总字节数 |数据包 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);
?>

最佳答案

也许这样就可以了:

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)

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

关于php - 从另一个表 SQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46144804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com