gpt4 book ai didi

php - 在连接查询 SQL 上全选

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

是否可以从连接查询中选择全部,我想从属于用户的数据包表中回显所有数据而不是连接每一列?

这是我的 sql 查询:

SELECT ip_address, SUM(upload_bytes) as upload_bytes, SUM(download_bytes) as 
download_bytes,
sum(upload_bytes + download_bytes) as totalbytes, package_id,
username
FROM
(
(SELECT ipaccounting.src_address as ip_address,
SUM(ipaccounting.bytes) AS upload_bytes, 0 as download_bytes,
users.username, datapackages.package_id
FROM ipaccounting
join users on users.ipaddress = ipaccounting.src_address
join datapackages on datapackages.package_id = users.datapackage
WHERE dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
GROUP BY src_address)
UNION ALL
(SELECT ipaccounting.dst_address as ip_address, 0 AS
upload_bytes, SUM(ipaccounting.bytes) as download_bytes,
users.username, datapackages.package_id
FROM ipaccounting
join users on users.ipaddress = ipaccounting.dst_address
join datapackages on datapackages.package_id = users.datapackage
WHERE src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
GROUP BY dst_address)
) a
GROUP BY ip_address
ORDER BY INET_ATON(ip_address)

我的完整代码:

<?php
//Include needed files
require ("config.php");
require ("includes/routeros_api.class.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,
sum(upload_bytes + download_bytes) as totalbytes, package_id,
username
FROM
(
(SELECT ipaccounting.src_address as ip_address,
SUM(ipaccounting.bytes) AS upload_bytes, 0 as download_bytes,
users.username, datapackages.package_id
FROM ipaccounting
join users on users.ipaddress = ipaccounting.src_address
join datapackages on datapackages.package_id = users.datapackage
WHERE dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
GROUP BY src_address)
UNION ALL
(SELECT ipaccounting.dst_address as ip_address, 0 AS
upload_bytes, SUM(ipaccounting.bytes) as download_bytes,
users.username, datapackages.package_id
FROM ipaccounting
join users on users.ipaddress = ipaccounting.dst_address
join datapackages on datapackages.package_id = users.datapackage
WHERE src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND
INET_NTOA('192.168.255.254')
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>
<th>Username</th><th>Data Package</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);

$username = $row['username'];
$datapackage = $row['package_id'];

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

//API MikroTik
/*$API = new RouterosAPI();

$API->debug = true;

if ($API->connect($RouterIP, $APIusername, $APIpassword)) {

$API->comm("/queue/simple/add", array(
"target-address="=> "10.10.10.16");

$API->disconnect();

}*/



}

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

我需要回显 IP 地址 |上传 |下载 |总计 |阈值 |下载速度 |上传速度 |数据限制

最佳答案

试试这个

SELECT ip_address, 
SUM(IF(ipaccounting.src_address != NULL, ipaccounting.bytes, 0)) as upload_bytes,
SUM(IF(ipaccounting.dst_address != NULL, ipaccounting.bytes, 0)) as download_bytes,
sum(ipaccounting.bytes) as totalbytes,
package_id, username
FROM ipaccounting
join users on users.ipaddress = ipaccounting.src_address OR users.ipaddress = ipaccounting.dst_address
join datapackages on datapackages.package_id = users.datapackage
WHERE ipaccounting.dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254')
AND ipaccounting.src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254')
GROUP BY src_address,dst_address ORDER BY INET_ATON(src_address),INET_ATON(dst_address)

为了更准确的查询,请贴出表结构和示例数据。

关于php - 在连接查询 SQL 上全选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46152555/

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