在连接查询SQL上选择全部

时间:2017-09-11 09:40:37

标签: php mysql

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

这是我的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地址|上传|下载|总计|门槛|下载速度|上传速度|数据限制

2 个答案:

答案 0 :(得分:0)

试试这个

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)

要获得更准确的查询,请发布表格结构和示例数据。

答案 1 :(得分:0)

我很久以前问这个问题时,已经不记得这是否是正确的答案,但是如果我能正确记住,我在寻找的答案与此类似:

SELECT 
    devices.*, 
    device_info.* 
FROM devices
LEFT JOIN device_info ON devices.id = device_info.device_id