SQL添加从一个表和多列

时间:2017-11-07 20:05:43

标签: mysql

我刚刚添加了几列(clients.site2_ipaddress clients.site3_ipaddress等等,直到clients.site10_ipaddress

我当前的查询,我尝试更改以添加额外的连接查询:

join clients on clients.site1_ipaddress 
             and clients.site2_ipaddress = ipaccounting.src_address

join clients on clients.site1_ipaddress 
             and clients.site2_ipaddress = ipaccounting.dst_address

输出将所有内容更改为site2_ipaddress并且只提供一行数据,我需要它为每列中的每个IP地址(site1_ipaddress,site2_ipaddress等)添加upload_bytes,download_bytes和total_bytes

对于我的生活,我无法弄清楚我做错了什么......

我还需要输出行以使用逗号和空格添加每个IP地址,例如:假设我的一个客户有两个网站,site1_ipaddress=192.168.1.2site2_ipaddress=192.168.1.3

我的输出需要是:

ip_address | upload_bytes | download_bytes | total_bytes | .....

192.168.1.2, 192.168.1.3 | upload_bytes? | download_bytes? | total_bytes? | .....

这可能吗?

我对额外联接的完整查询是:

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,
    userid,
    networkaccess,
    packagename,
    speedlimit,
    threshold,
    throttlelimit,
    extendeddata,
    datalimitamount,
    accountingdays
FROM
    (
        (
        SELECT
            ipaccounting.src_address AS ip_address,
            SUM(ipaccounting.bytes) AS upload_bytes,
            0 AS download_bytes,
            clients.username,
            clients.userid,
            clients.networkaccess,
            clients.extendeddata,
            datapackages.package_id,
            datapackages.packagename,
            datapackages.speedlimit,
            datapackages.threshold,
            datapackages.throttlelimit,
            datapackages.datalimitamount,
            datapackages.accountingdays
        FROM
            ipaccounting
        JOIN clients ON clients.ipaddress AND clients.`site2_ipaddress` = ipaccounting.src_address
    JOIN datapackages ON datapackages.package_id = clients.datapackage
    WHERE
        dst_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254') AND timeanddate BETWEEN SUBDATE(
            CURRENT_TIMESTAMP(), INTERVAL datapackages.accountingdays DAY) AND CURRENT_TIMESTAMP()
        GROUP BY
            src_address)
        UNION ALL
            (
            SELECT
                ipaccounting.dst_address AS ip_address,
                0 AS upload_bytes,
                SUM(ipaccounting.bytes) AS download_bytes,
                clients.username,
                clients.userid,
                clients.networkaccess,
                clients.extendeddata,
                datapackages.package_id,
                datapackages.packagename,
                datapackages.speedlimit,
                datapackages.threshold,
                datapackages.throttlelimit,
                datapackages.datalimitamount,
                datapackages.accountingdays
            FROM
                ipaccounting
            JOIN clients ON clients.ipaddress AND clients.`site2_ipaddress` = ipaccounting.dst_address
            JOIN datapackages ON datapackages.package_id = clients.datapackage
            WHERE
                src_address NOT BETWEEN INET_NTOA('192.168.0.1') AND INET_NTOA('192.168.255.254') AND timeanddate BETWEEN SUBDATE(
                    CURRENT_TIMESTAMP(), INTERVAL datapackages.accountingdays DAY) AND CURRENT_TIMESTAMP()
                GROUP BY
                    dst_address)
                ) a
            GROUP BY
                ip_address
            ORDER BY
                INET_ATON(ip_address)

客户表:

Clients Table

IP会计表:

IP Accounting Table

1 个答案:

答案 0 :(得分:1)

我很遗憾地说,在添加这些额外的列时你做了一个糟糕的选择。你真的需要创建一个新的表来处理这种多对一的关系(许多站点ip到一个客户端)

无法执行此操作:join clients on clients.site1_ipaddress and clients.site2_ipaddress = ipaccounting.dst_address

你必须这样做:

join clients on clients.site1_ipaddress  = ipaccounting.dst_address
    OR clients.site2_ipaddress = ipaccounting.dst_address
    OR clients.site3_ipaddress = ipaccounting.dst_address
    OR clients.site4_ipaddress = ipaccounting.dst_address
    OR clients.site5_ipaddress = ipaccounting.dst_address
    OR clients.site6_ipaddress = ipaccounting.dst_address
    OR clients.site7_ipaddress = ipaccounting.dst_address
    OR clients.site8_ipaddress = ipaccounting.dst_address
    OR clients.site9_ipaddress = ipaccounting.dst_address
    OR clients.site10_ipaddress = ipaccounting.dst_address

并且每次你需要那个"旋转" ip信息分布在10列中,在每个查询中都注定了这种复杂性。或者,创建这样的新表:

client_site_ips
id client_id ipaddress

然后,在我们的查询中,您可以对该表中的行使用GROUP_CONCAT()来显示逗号分隔列表 - 如果这是您想要的。

----

client_site_ip 
siteid, userid, site_name, site_ip

客户端ID由userid列

指定
select *
from client_site_ip csi 
join clients c on csi.client_site_ip = c.userid

类似这样的事情