我正在尝试将多行数据连接到sql查询。
我的数据库包含几个表:clients
,client_site_ip
和ip_accounting
。
client_site_ip
表格包含以下行:siteid
,userid
,site_name
和site_ip
。
ip_accounting
表格包含以下行:id
,src_address
,dst_address
,bytes
和timeanddate
。
clients
表格包含以下行:userid
,username
......和ipaddress
我希望客户拥有多个IP地址,因此我创建了表client_site_ip
我需要查询数据库并为每个ip地址获取多行数据: userid |用户名| datapackage | sitename | ipaddress | packagename | speedlimit ........列表更进一步。
我目前有这个查询用逗号分隔IP地址和所有内容,但需要用新数据行分隔,而不是用逗号分隔:
SELECT
clients.userid,
clients.username,
clients.datapackage,
client_site_ip.userid,
GROUP_CONCAT(client_site_ip.site_name) AS sitenames,
GROUP_CONCAT(client_site_ip.site_ip) AS ipaddresses,
data_packages.packagename,
data_packages.speedlimit,
data_packages.threshold,
data_packages.accountingdays,
data_packages.throttlelimit,
data_packages.datalimitamount
FROM
clients
JOIN client_site_ip ON clients.userid = client_site_ip.userid
JOIN data_packages ON clients.datapackage = data_packages.package_id
GROUP BY
clients.userid
我还需要更新同步查询,当前脚本如下:
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 ip_accounting.src_address as ip_address
,SUM(ip_accounting.bytes) AS upload_bytes
,0 as download_bytes
,clients.username
,clients.userid
,clients.networkaccess
,clients.extendeddata
,data_packages.package_id
,data_packages.packagename
,data_packages.speedlimit
,data_packages.threshold
,data_packages.throttlelimit
,data_packages.datalimitamount
,data_packages.accountingdays
FROM ip_accounting
join clients on clients.ipaddress = ip_accounting.src_address
join data_packages on data_packages.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
data_packages.accountingdays DAY) AND CURRENT_TIMESTAMP()
GROUP BY src_address)
UNION ALL
(SELECT ip_accounting.dst_address as ip_address
,0 AS upload_bytes
,SUM(ip_accounting.bytes) as download_bytes
,clients.username
,clients.userid
,clients.networkaccess
,clients.extendeddata
,data_packages.package_id
,data_packages.packagename
,data_packages.speedlimit
,data_packages.threshold
,data_packages.throttlelimit
,data_packages.datalimitamount
,data_packages.accountingdays
FROM ip_accounting
join clients on clients.ipaddress = ip_accounting.dst_address
join data_packages on data_packages.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 data_packages.accountingdays DAY) AND CURRENT_TIMESTAMP()
GROUP BY dst_address)
) a
GROUP BY ip_address
ORDER BY INET_ATON(ip_address)
答案 0 :(得分:1)
如果您想为每个site_ip
和site_name
分别设置行,您也可以对这些列进行分组
SELECT
clients.userid,
clients.username,
clients.datapackage,
client_site_ip.userid,
client_site_ip.site_name AS sitenames,
client_site_ip.site_ip AS ipaddresses,
data_packages.packagename,
data_packages.speedlimit,
data_packages.threshold,
data_packages.accountingdays,
data_packages.throttlelimit,
data_packages.datalimitamount
FROM
clients
JOIN client_site_ip ON clients.userid = client_site_ip.userid
JOIN data_packages ON clients.datapackage = data_packages.package_id
GROUP BY
clients.userid, client_site_ip.site_name, client_site_ip.site_ip
就您的同步查询而言,我不相信您需要任何更新,因为所有联接已经在ipaddress
等,而不是group_concat